home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / stk-3.002 / stk-3 / STk-3.1 / Tk / unix / tkUnixWm.c < prev   
Encoding:
C/C++ Source or Header  |  1996-07-23  |  125.2 KB  |  4,248 lines

  1. /* 
  2.  * tkUnixWm.c --
  3.  *
  4.  *    This module takes care of the interactions between a Tk-based
  5.  *    application and the window manager.  Among other things, it
  6.  *    implements the "wm" command and passes geometry information
  7.  *    to the window manager.
  8.  *
  9.  * Copyright (c) 1991-1994 The Regents of the University of California.
  10.  * Copyright (c) 1994-1996 Sun Microsystems, Inc.
  11.  *
  12.  * See the file "license.terms" for information on usage and redistribution
  13.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  14.  *
  15.  * SCCS: @(#) tkUnixWm.c 1.124 96/03/29 14:05:44
  16.  */
  17.  
  18. #include "tkPort.h"
  19. #include "tkInt.h"
  20. #include <errno.h>
  21.  
  22. /*
  23.  * A data structure of the following type holds information for
  24.  * each window manager protocol (such as WM_DELETE_WINDOW) for
  25.  * which a handler (i.e. a Tcl command) has been defined for a
  26.  * particular top-level window.
  27.  */
  28.  
  29. typedef struct ProtocolHandler {
  30.     Atom protocol;        /* Identifies the protocol. */
  31.     struct ProtocolHandler *nextPtr;
  32.                 /* Next in list of protocol handlers for
  33.                  * the same top-level window, or NULL for
  34.                  * end of list. */
  35.     Tcl_Interp *interp;        /* Interpreter in which to invoke command. */
  36.     char command[4];        /* Tcl command to invoke when a client
  37.                  * message for this protocol arrives. 
  38.                  * The actual size of the structure varies
  39.                  * to accommodate the needs of the actual
  40.                  * command. THIS MUST BE THE LAST FIELD OF
  41.                  * THE STRUCTURE. */
  42. } ProtocolHandler;
  43.  
  44. #define HANDLER_SIZE(cmdLength) \
  45.     ((unsigned) (sizeof(ProtocolHandler) - 3 + cmdLength))
  46.  
  47. /*
  48.  * A data structure of the following type holds window-manager-related
  49.  * information for each top-level window in an application.
  50.  */
  51.  
  52. typedef struct TkWmInfo {
  53.     TkWindow *winPtr;        /* Pointer to main Tk information for
  54.                  * this window. */
  55.     Window reparent;        /* If the window has been reparented, this
  56.                  * gives the ID of the ancestor of the window
  57.                  * that is a child of the root window (may
  58.                  * not be window's immediate parent).  If
  59.                  * the window isn't reparented, this has the
  60.                  * value None. */
  61.     Tk_Uid titleUid;        /* Title to display in window caption.  If
  62.                  * NULL, use name of widget. */
  63.     Tk_Uid iconName;        /* Name to display in icon. */
  64.     Window master;        /* Master window for TRANSIENT_FOR property,
  65.                  * or None. */
  66.     XWMHints hints;        /* Various pieces of information for
  67.                  * window manager. */
  68.     Tk_Uid leaderName;        /* Path name of leader of window group
  69.                  * (corresponds to hints.window_group).
  70.                  * Note:  this field doesn't get updated
  71.                  * if leader is destroyed. */
  72.     Tk_Uid masterWindowName;    /* Path name of window specified as master
  73.                  * in "wm transient" command, or NULL.
  74.                  * Note:  this field doesn't get updated if
  75.                  * masterWindowName is destroyed. */
  76.     Tk_Window icon;        /* Window to use as icon for this window,
  77.                  * or NULL. */
  78.     Tk_Window iconFor;        /* Window for which this window is icon, or
  79.                  * NULL if this isn't an icon for anyone. */
  80.     int withdrawn;        /* Non-zero means window has been withdrawn. */
  81.  
  82.     /*
  83.      * Information used to construct an XSizeHints structure for
  84.      * the window manager:
  85.      */
  86.  
  87.     int sizeHintsFlags;        /* Flags word for XSizeHints structure.
  88.                  * If the PBaseSize flag is set then the
  89.                  * window is gridded;  otherwise it isn't
  90.                  * gridded. */
  91.     int minWidth, minHeight;    /* Minimum dimensions of window, in
  92.                  * grid units, not pixels. */
  93.     int maxWidth, maxHeight;    /* Maximum dimensions of window, in
  94.                  * grid units, not pixels. */
  95.     Tk_Window gridWin;        /* Identifies the window that controls
  96.                  * gridding for this top-level, or NULL if
  97.                  * the top-level isn't currently gridded. */
  98.     int widthInc, heightInc;    /* Increments for size changes (# pixels
  99.                  * per step). */
  100.     struct {
  101.     int x;    /* numerator */
  102.     int y;  /* denominator */
  103.     } minAspect, maxAspect;    /* Min/max aspect ratios for window. */
  104.     int reqGridWidth, reqGridHeight;
  105.                 /* The dimensions of the window (in
  106.                  * grid units) requested through
  107.                  * the geometry manager. */
  108.     int gravity;        /* Desired window gravity. */
  109.  
  110.     /*
  111.      * Information used to manage the size and location of a window.
  112.      */
  113.  
  114.     int width, height;        /* Desired dimensions of window, specified
  115.                  * in grid units.  These values are
  116.                  * set by the "wm geometry" command and by
  117.                  * ConfigureNotify events (for when wm
  118.                  * resizes window).  -1 means user hasn't
  119.                  * requested dimensions. */
  120.     int x, y;            /* Desired X and Y coordinates for window.
  121.                  * These values are set by "wm geometry",
  122.                  * plus by ConfigureNotify events (when wm
  123.                  * moves window).  These numbers are
  124.                  * different than the numbers stored in
  125.                  * winPtr->changes because (a) they could be
  126.                  * measured from the right or bottom edge
  127.                  * of the screen (see WM_NEGATIVE_X and
  128.                  * WM_NEGATIVE_Y flags) and (b) if the window
  129.                  * has been reparented then they refer to the
  130.                  * parent rather than the window itself. */
  131.     int parentWidth, parentHeight;
  132.                 /* Width and height of reparent, in pixels
  133.                  * *including border*.  If window hasn't been
  134.                  * reparented then these will be the outer
  135.                  * dimensions of the window, including
  136.                  * border. */
  137.     int xInParent, yInParent;    /* Offset of window within reparent,  measured
  138.                  * from upper-left outer corner of parent's
  139.                  * border to upper-left outer corner of child's
  140.                  * border.  If not reparented then these are
  141.                  * zero. */
  142.     int configWidth, configHeight;
  143.                 /* Dimensions passed to last request that we
  144.                  * issued to change geometry of window.  Used
  145.                  * to eliminate redundant resize operations. */
  146.  
  147.     /*
  148.      * Information about the virtual root window for this top-level,
  149.      * if there is one.
  150.      */
  151.  
  152.     Window vRoot;        /* Virtual root window for this top-level,
  153.                  * or None if there is no virtual root
  154.                  * window (i.e. just use the screen's root). */
  155.     int vRootX, vRootY;        /* Position of the virtual root inside the
  156.                  * root window.  If the WM_VROOT_OFFSET_STALE
  157.                  * flag is set then this information may be
  158.                  * incorrect and needs to be refreshed from
  159.                  * the X server.  If vRoot is None then these
  160.                  * values are both 0. */
  161.     int vRootWidth, vRootHeight;/* Dimensions of the virtual root window.
  162.                  * If vRoot is None, gives the dimensions
  163.                  * of the containing screen.  This information
  164.                  * is never stale, even though vRootX and
  165.                  * vRootY can be. */
  166.  
  167.     /*
  168.      * Miscellaneous information.
  169.      */
  170.  
  171.     ProtocolHandler *protPtr;    /* First in list of protocol handlers for
  172.                  * this window (NULL means none). */
  173.     int cmdArgc;        /* Number of elements in cmdArgv below. */
  174.     char **cmdArgv;        /* Array of strings to store in the
  175.                  * WM_COMMAND property.  NULL means nothing
  176.                  * available. */
  177.     char *clientMachine;    /* String to store in WM_CLIENT_MACHINE
  178.                  * property, or NULL. */
  179.     int flags;            /* Miscellaneous flags, defined below. */
  180.     struct TkWmInfo *nextPtr;    /* Next in list of all top-level windows. */
  181. } WmInfo;
  182.  
  183. /*
  184.  * Flag values for WmInfo structures:
  185.  *
  186.  * WM_NEVER_MAPPED -        non-zero means window has never been
  187.  *                mapped;  need to update all info when
  188.  *                window is first mapped.
  189.  * WM_UPDATE_PENDING -        non-zero means a call to UpdateGeometryInfo
  190.  *                has already been scheduled for this
  191.  *                window;  no need to schedule another one.
  192.  * WM_NEGATIVE_X -        non-zero means x-coordinate is measured in
  193.  *                pixels from right edge of screen, rather
  194.  *                than from left edge.
  195.  * WM_NEGATIVE_Y -        non-zero means y-coordinate is measured in
  196.  *                pixels up from bottom of screen, rather than
  197.  *                down from top.
  198.  * WM_UPDATE_SIZE_HINTS -    non-zero means that new size hints need to be
  199.  *                propagated to window manager.
  200.  * WM_SYNC_PENDING -        set to non-zero while waiting for the window
  201.  *                manager to respond to some state change.
  202.  * WM_VROOT_OFFSET_STALE -    non-zero means that (x,y) offset information
  203.  *                about the virtual root window is stale and
  204.  *                needs to be fetched fresh from the X server.
  205.  * WM_ABOUT_TO_MAP -        non-zero means that the window is about to
  206.  *                be mapped by TkWmMapWindow.  This is used
  207.  *                by UpdateGeometryInfo to modify its behavior.
  208.  * WM_MOVE_PENDING -        non-zero means the application has requested
  209.  *                a new position for the window, but it hasn't
  210.  *                been reflected through the window manager
  211.  *                yet.
  212.  * WM_COLORMAPS_EXPLICIT -    non-zero means the colormap windows were
  213.  *                set explicitly via "wm colormapwindows".
  214.  * WM_ADDED_TOPLEVEL_COLORMAP - non-zero means that when "wm colormapwindows"
  215.  *                was called the top-level itself wasn't
  216.  *                specified, so we added it implicitly at
  217.  *                the end of the list.
  218.  * WM_WIDTH_NOT_RESIZABLE -    non-zero means that we're not supposed to
  219.  *                allow the user to change the width of the
  220.  *                window (controlled by "wm resizable"
  221.  *                command).
  222.  * WM_HEIGHT_NOT_RESIZABLE -    non-zero means that we're not supposed to
  223.  *                allow the user to change the height of the
  224.  *                window (controlled by "wm resizable"
  225.  *                command).
  226.  */
  227.  
  228. #define WM_NEVER_MAPPED            1
  229. #define WM_UPDATE_PENDING        2
  230. #define WM_NEGATIVE_X            4
  231. #define WM_NEGATIVE_Y            8
  232. #define WM_UPDATE_SIZE_HINTS        0x10
  233. #define WM_SYNC_PENDING            0x20
  234. #define WM_VROOT_OFFSET_STALE        0x40
  235. #define WM_ABOUT_TO_MAP            0x100
  236. #define WM_MOVE_PENDING            0x200
  237. #define WM_COLORMAPS_EXPLICIT        0x400
  238. #define WM_ADDED_TOPLEVEL_COLORMAP    0x800
  239. #define WM_WIDTH_NOT_RESIZABLE        0x1000
  240. #define WM_HEIGHT_NOT_RESIZABLE        0x2000
  241.  
  242. /*
  243.  * This module keeps a list of all top-level windows, primarily to
  244.  * simplify the job of Tk_CoordsToWindow.
  245.  */
  246.  
  247. static WmInfo *firstWmPtr = NULL;    /* Points to first top-level window. */
  248.  
  249.  
  250. /*
  251.  * The variable below is used to enable or disable tracing in this
  252.  * module.  If tracing is enabled, then information is printed on
  253.  * standard output about interesting interactions with the window
  254.  * manager.
  255.  */
  256.  
  257. static int wmTracing = 0;
  258.  
  259. /*
  260.  * The following structure is the official type record for geometry
  261.  * management of top-level windows.
  262.  */
  263.  
  264. static void        TopLevelReqProc _ANSI_ARGS_((ClientData dummy,
  265.                 Tk_Window tkwin));
  266.  
  267. static Tk_GeomMgr wmMgrType = {
  268.     "wm",                /* name */
  269.     TopLevelReqProc,            /* requestProc */
  270.     (Tk_GeomLostSlaveProc *) NULL,    /* lostSlaveProc */
  271. };
  272.  
  273. /*
  274.  * Structures of the following type are used for communication between
  275.  * WaitForEvent, WaitRestrictProc, and WaitTimeoutProc.
  276.  */
  277.  
  278. typedef struct WaitRestrictInfo {
  279.     Display *display;        /* Window belongs to this display. */
  280.     Window window;        /* We're waiting for events on this window. */
  281.     int type;            /* We only care about this type of event. */
  282.     XEvent *eventPtr;        /* Where to store the event when it's found. */
  283.     int foundEvent;        /* Non-zero means that an event of the
  284.                  * desired type has been found. */
  285.     int timeout;        /* Non-zero means that too much time elapsed
  286.                  * while waiting, and we should just give
  287.                  * up. */
  288. } WaitRestrictInfo;
  289.  
  290. /*
  291.  * Forward declarations for procedures defined in this file:
  292.  */
  293.  
  294. static int        ComputeReparentGeometry _ANSI_ARGS_((TkWindow *winPtr));
  295. static void        ConfigureEvent _ANSI_ARGS_((TkWindow *winPtr,
  296.                 XConfigureEvent *eventPtr));
  297. static void        GetMaxSize _ANSI_ARGS_((WmInfo *wmPtr,
  298.                 int *maxWidthPtr, int *maxHeightPtr));
  299. static int        ParseGeometry _ANSI_ARGS_((Tcl_Interp *interp,
  300.                 char *string, TkWindow *winPtr));
  301. static void        ReparentEvent _ANSI_ARGS_((TkWindow *winPtr,
  302.                 XReparentEvent *eventPtr));
  303. static void        TopLevelEventProc _ANSI_ARGS_((ClientData clientData,
  304.                 XEvent *eventPtr));
  305. static void        TopLevelReqProc _ANSI_ARGS_((ClientData dummy,
  306.                 Tk_Window tkwin));
  307. static void        UpdateGeometryInfo _ANSI_ARGS_((
  308.                 ClientData clientData));
  309. static void        UpdateHints _ANSI_ARGS_((TkWindow *winPtr));
  310. static void        UpdateSizeHints _ANSI_ARGS_((TkWindow *winPtr));
  311. static void        UpdateVRootGeometry _ANSI_ARGS_((WmInfo *wmPtr));
  312. static void        UpdateWmProtocols _ANSI_ARGS_((WmInfo *wmPtr));
  313. static void        WaitForConfigureNotify _ANSI_ARGS_((TkWindow *winPtr,
  314.                 unsigned long serial));
  315. static int        WaitForEvent _ANSI_ARGS_((Display *display,
  316.                 Window window, int type, XEvent *eventPtr));
  317. static void        WaitForMapNotify _ANSI_ARGS_((TkWindow *winPtr,
  318.                 int mapped));
  319. static Tk_RestrictAction
  320.             WaitRestrictProc _ANSI_ARGS_((ClientData clientData,
  321.                 XEvent *eventPtr));
  322. static void        WaitTimeoutProc _ANSI_ARGS_((ClientData clientData));
  323.  
  324. /*
  325.  *--------------------------------------------------------------
  326.  *
  327.  * TkWmNewWindow --
  328.  *
  329.  *    This procedure is invoked whenever a new top-level
  330.  *    window is created.  Its job is to initialize the WmInfo
  331.  *    structure for the window.
  332.  *
  333.  * Results:
  334.  *    None.
  335.  *
  336.  * Side effects:
  337.  *    A WmInfo structure gets allocated and initialized.
  338.  *
  339.  *--------------------------------------------------------------
  340.  */
  341.  
  342. void
  343. TkWmNewWindow(winPtr)
  344.     TkWindow *winPtr;        /* Newly-created top-level window. */
  345. {
  346.     register WmInfo *wmPtr;
  347.  
  348.     wmPtr = (WmInfo *) ckalloc(sizeof(WmInfo));
  349.     wmPtr->winPtr = winPtr;
  350.     wmPtr->reparent = None;
  351.     wmPtr->titleUid = NULL;
  352.     wmPtr->iconName = NULL;
  353.     wmPtr->master = None;
  354.     wmPtr->hints.flags = InputHint | StateHint;
  355.     wmPtr->hints.input = True;
  356.     wmPtr->hints.initial_state = NormalState;
  357.     wmPtr->hints.icon_pixmap = None;
  358.     wmPtr->hints.icon_window = None;
  359.     wmPtr->hints.icon_x = wmPtr->hints.icon_y = 0;
  360.     wmPtr->hints.icon_mask = None;
  361.     wmPtr->hints.window_group = None;
  362.     wmPtr->leaderName = NULL;
  363.     wmPtr->masterWindowName = NULL;
  364.     wmPtr->icon = NULL;
  365.     wmPtr->iconFor = NULL;
  366.     wmPtr->withdrawn = 0;
  367.     wmPtr->sizeHintsFlags = 0;
  368.     wmPtr->minWidth = wmPtr->minHeight = 1;
  369.  
  370.     /*
  371.      * Default the maximum dimensions to the size of the display, minus
  372.      * a guess about how space is needed for window manager decorations.
  373.      */
  374.  
  375.     wmPtr->maxWidth = 0;
  376.     wmPtr->maxHeight = 0;
  377.     wmPtr->gridWin = NULL;
  378.     wmPtr->widthInc = wmPtr->heightInc = 1;
  379.     wmPtr->minAspect.x = wmPtr->minAspect.y = 1;
  380.     wmPtr->maxAspect.x = wmPtr->maxAspect.y = 1;
  381.     wmPtr->reqGridWidth = wmPtr->reqGridHeight = -1;
  382.     wmPtr->gravity = NorthWestGravity;
  383.     wmPtr->width = -1;
  384.     wmPtr->height = -1;
  385.     wmPtr->x = winPtr->changes.x;
  386.     wmPtr->y = winPtr->changes.y;
  387.     wmPtr->parentWidth = winPtr->changes.width
  388.         + 2*winPtr->changes.border_width;
  389.     wmPtr->parentHeight = winPtr->changes.height
  390.         + 2*winPtr->changes.border_width;
  391.     wmPtr->xInParent = wmPtr->yInParent = 0;
  392.     wmPtr->configWidth = -1;
  393.     wmPtr->configHeight = -1;
  394.     wmPtr->vRoot = None;
  395.     wmPtr->protPtr = NULL;
  396.     wmPtr->cmdArgv = NULL;
  397.     wmPtr->clientMachine = NULL;
  398.     wmPtr->flags = WM_NEVER_MAPPED;
  399.     wmPtr->nextPtr = firstWmPtr;
  400.     firstWmPtr = wmPtr;
  401.     winPtr->wmInfoPtr = wmPtr;
  402.  
  403.     UpdateVRootGeometry(wmPtr);
  404.  
  405.     /*
  406.      * Tk must monitor structure events for top-level windows, in order
  407.      * to detect size and position changes caused by window managers.
  408.      */
  409.  
  410.     Tk_CreateEventHandler((Tk_Window) winPtr, StructureNotifyMask,
  411.         TopLevelEventProc, (ClientData) winPtr);
  412.  
  413.     /*
  414.      * Arrange for geometry requests to be reflected from the window
  415.      * to the window manager.
  416.      */
  417.  
  418.     Tk_ManageGeometry((Tk_Window) winPtr, &wmMgrType, (ClientData) 0);
  419. }
  420.  
  421. /*
  422.  *--------------------------------------------------------------
  423.  *
  424.  * TkWmMapWindow --
  425.  *
  426.  *    This procedure is invoked to map a top-level window.  This
  427.  *    module gets a chance to update all window-manager-related
  428.  *    information in properties before the window manager sees
  429.  *    the map event and checks the properties.  It also gets to
  430.  *    decide whether or not to even map the window after all.
  431.  *
  432.  * Results:
  433.  *    None.
  434.  *
  435.  * Side effects:
  436.  *    Properties of winPtr may get updated to provide up-to-date
  437.  *    information to the window manager.  The window may also get
  438.  *    mapped, but it may not be if this procedure decides that
  439.  *    isn't appropriate (e.g. because the window is withdrawn).
  440.  *
  441.  *--------------------------------------------------------------
  442.  */
  443.  
  444. void
  445. TkWmMapWindow(winPtr)
  446.     TkWindow *winPtr;        /* Top-level window that's about to
  447.                  * be mapped. */
  448. {
  449.     register WmInfo *wmPtr = winPtr->wmInfoPtr;
  450.     XTextProperty textProp;
  451.  
  452.     if (wmPtr->flags & WM_NEVER_MAPPED) {
  453.     wmPtr->flags &= ~WM_NEVER_MAPPED;
  454.  
  455.     /*
  456.      * This is the first time this window has ever been mapped.
  457.      * Store all the window-manager-related information for the
  458.      * window.
  459.      */
  460.  
  461.     if (wmPtr->titleUid == NULL) {
  462.         wmPtr->titleUid = winPtr->nameUid;
  463.     }
  464.     if (XStringListToTextProperty(&wmPtr->titleUid, 1, &textProp)  != 0) {
  465.         XSetWMName(winPtr->display, winPtr->window, &textProp);
  466.         XFree((char *) textProp.value);
  467.     }
  468.     
  469.     TkWmSetClass(winPtr);
  470.     
  471.     if (wmPtr->iconName != NULL) {
  472.         XSetIconName(winPtr->display, winPtr->window, wmPtr->iconName);
  473.     }
  474.     
  475.     if (wmPtr->master != None) {
  476.         XSetTransientForHint(winPtr->display, winPtr->window,
  477.             wmPtr->master);
  478.     }
  479.     
  480.     wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
  481.     UpdateHints(winPtr);
  482.     UpdateWmProtocols(wmPtr);
  483.     if (wmPtr->cmdArgv != NULL) {
  484.         XSetCommand(winPtr->display, winPtr->window, wmPtr->cmdArgv,
  485.             wmPtr->cmdArgc);
  486.     }
  487.     if (wmPtr->clientMachine != NULL) {
  488.         if (XStringListToTextProperty(&wmPtr->clientMachine, 1, &textProp)
  489.             != 0) {
  490.         XSetWMClientMachine(winPtr->display, winPtr->window,
  491.             &textProp);
  492.         XFree((char *) textProp.value);
  493.         }
  494.     }
  495.     }
  496.     if (wmPtr->hints.initial_state == WithdrawnState) {
  497.     return;
  498.     }
  499.     if (wmPtr->iconFor != NULL) {
  500.     /*
  501.      * This window is an icon for somebody else.  Make sure that
  502.      * the geometry is up-to-date, then return without mapping
  503.      * the window.
  504.      */
  505.  
  506.     if (wmPtr->flags & WM_UPDATE_PENDING) {
  507.         Tcl_CancelIdleCall(UpdateGeometryInfo, (ClientData) winPtr);
  508.     }
  509.     UpdateGeometryInfo((ClientData) winPtr);
  510.     return;
  511.     }
  512.     wmPtr->flags |= WM_ABOUT_TO_MAP;
  513.     if (wmPtr->flags & WM_UPDATE_PENDING) {
  514.     Tcl_CancelIdleCall(UpdateGeometryInfo, (ClientData) winPtr);
  515.     }
  516.     UpdateGeometryInfo((ClientData) winPtr);
  517.     wmPtr->flags &= ~WM_ABOUT_TO_MAP;
  518.  
  519.     /*
  520.      * Map the window, then wait to be sure that the window manager has
  521.      * processed the map operation.
  522.      */
  523.  
  524.     XMapWindow(winPtr->display, winPtr->window);
  525.     if (wmPtr->hints.initial_state == NormalState) {
  526.     WaitForMapNotify(winPtr, 1);
  527.     }
  528. }
  529.  
  530. /*
  531.  *--------------------------------------------------------------
  532.  *
  533.  * TkWmUnmapWindow --
  534.  *
  535.  *    This procedure is invoked to unmap a top-level window.  The
  536.  *    only thing it does special is to wait for the window actually
  537.  *    to be unmapped.
  538.  *
  539.  * Results:
  540.  *    None.
  541.  *
  542.  * Side effects:
  543.  *    Unmaps the window.
  544.  *
  545.  *--------------------------------------------------------------
  546.  */
  547.  
  548. void
  549. TkWmUnmapWindow(winPtr)
  550.     TkWindow *winPtr;        /* Top-level window that's about to
  551.                  * be mapped. */
  552. {
  553.     /*
  554.      * It seems to be important to wait after unmapping a top-level
  555.      * window until the window really gets unmapped.  I don't completely
  556.      * understand all the interactions with the window manager, but if
  557.      * we go on without waiting, and if the window is then mapped again
  558.      * quickly, events seem to get lost so that we think the window isn't
  559.      * mapped when in fact it is mapped.  I suspect that this has something
  560.      * to do with the window manager filtering Map events (and possily not
  561.      * filtering Unmap events?).
  562.      */ 
  563.     XUnmapWindow(winPtr->display, winPtr->window);
  564.     WaitForMapNotify(winPtr, 0);
  565. }
  566.  
  567. /*
  568.  *--------------------------------------------------------------
  569.  *
  570.  * TkWmDeadWindow --
  571.  *
  572.  *    This procedure is invoked when a top-level window is
  573.  *    about to be deleted.  It cleans up the wm-related data
  574.  *    structures for the window.
  575.  *
  576.  * Results:
  577.  *    None.
  578.  *
  579.  * Side effects:
  580.  *    The WmInfo structure for winPtr gets freed up.
  581.  *
  582.  *--------------------------------------------------------------
  583.  */
  584.  
  585. void
  586. TkWmDeadWindow(winPtr)
  587.     TkWindow *winPtr;        /* Top-level window that's being deleted. */
  588. {
  589.     register WmInfo *wmPtr = winPtr->wmInfoPtr;
  590.     WmInfo *wmPtr2;
  591.  
  592.     if (wmPtr == NULL) {
  593.     return;
  594.     }
  595.     if (firstWmPtr == wmPtr) {
  596.     firstWmPtr = wmPtr->nextPtr;
  597.     } else {
  598.     register WmInfo *prevPtr;
  599.  
  600.     for (prevPtr = firstWmPtr; ; prevPtr = prevPtr->nextPtr) {
  601.         if (prevPtr == NULL) {
  602.         panic("couldn't unlink window in TkWmDeadWindow");
  603.         }
  604.         if (prevPtr->nextPtr == wmPtr) {
  605.         prevPtr->nextPtr = wmPtr->nextPtr;
  606.         break;
  607.         }
  608.     }
  609.     }
  610.     if (wmPtr->hints.flags & IconPixmapHint) {
  611.     Tk_FreeBitmap(winPtr->display, wmPtr->hints.icon_pixmap);
  612.     }
  613.     if (wmPtr->hints.flags & IconMaskHint) {
  614.     Tk_FreeBitmap(winPtr->display, wmPtr->hints.icon_mask);
  615.     }
  616.     if (wmPtr->icon != NULL) {
  617.     wmPtr2 = ((TkWindow *) wmPtr->icon)->wmInfoPtr;
  618.     wmPtr2->iconFor = NULL;
  619.     wmPtr2->withdrawn = 1;
  620.     }
  621.     if (wmPtr->iconFor != NULL) {
  622.     wmPtr2 = ((TkWindow *) wmPtr->iconFor)->wmInfoPtr;
  623.     wmPtr2->icon = NULL;
  624.     wmPtr2->hints.flags &= ~IconWindowHint;
  625.     UpdateHints((TkWindow *) wmPtr->iconFor);
  626.     }
  627.     while (wmPtr->protPtr != NULL) {
  628.     ProtocolHandler *protPtr;
  629.  
  630.     protPtr = wmPtr->protPtr;
  631.     wmPtr->protPtr = protPtr->nextPtr;
  632.     Tcl_EventuallyFree((ClientData) protPtr, TCL_DYNAMIC);
  633.     }
  634.     if (wmPtr->cmdArgv != NULL) {
  635.     ckfree((char *) wmPtr->cmdArgv);
  636.     }
  637.     if (wmPtr->clientMachine != NULL) {
  638.     ckfree((char *) wmPtr->clientMachine);
  639.     }
  640.     if (wmPtr->flags & WM_UPDATE_PENDING) {
  641.     Tcl_CancelIdleCall(UpdateGeometryInfo, (ClientData) winPtr);
  642.     }
  643.     ckfree((char *) wmPtr);
  644.     winPtr->wmInfoPtr = NULL;
  645. }
  646.  
  647. /*
  648.  *--------------------------------------------------------------
  649.  *
  650.  * TkWmSetClass --
  651.  *
  652.  *    This procedure is invoked whenever a top-level window's
  653.  *    class is changed.  If the window has been mapped then this
  654.  *    procedure updates the window manager property for the
  655.  *    class.  If the window hasn't been mapped, the update is
  656.  *    deferred until just before the first mapping.
  657.  *
  658.  * Results:
  659.  *    None.
  660.  *
  661.  * Side effects:
  662.  *    A window property may get updated.
  663.  *
  664.  *--------------------------------------------------------------
  665.  */
  666.  
  667. void
  668. TkWmSetClass(winPtr)
  669.     TkWindow *winPtr;        /* Newly-created top-level window. */
  670. {
  671.     if (winPtr->wmInfoPtr->flags & WM_NEVER_MAPPED) {
  672.     return;
  673.     }
  674.  
  675.     if (winPtr->classUid != NULL) {
  676.     XClassHint *classPtr;
  677.  
  678.     classPtr = XAllocClassHint();
  679.     classPtr->res_name = winPtr->nameUid;
  680.     classPtr->res_class = winPtr->classUid;
  681.     XSetClassHint(winPtr->display, winPtr->window, classPtr);
  682.     XFree((char *) classPtr);
  683.     }
  684. }
  685.  
  686. /*
  687.  *----------------------------------------------------------------------
  688.  *
  689.  * Tk_WmCmd --
  690.  *
  691.  *    This procedure is invoked to process the "wm" Tcl command.
  692.  *    See the user documentation for details on what it does.
  693.  *
  694.  * Results:
  695.  *    A standard Tcl result.
  696.  *
  697.  * Side effects:
  698.  *    See the user documentation.
  699.  *
  700.  *----------------------------------------------------------------------
  701.  */
  702.  
  703.     /* ARGSUSED */
  704. int
  705. Tk_WmCmd(clientData, interp, argc, argv)
  706.     ClientData clientData;    /* Main window associated with
  707.                  * interpreter. */
  708.     Tcl_Interp *interp;        /* Current interpreter. */
  709.     int argc;            /* Number of arguments. */
  710.     char **argv;        /* Argument strings. */
  711. {
  712.     Tk_Window tkwin = (Tk_Window) clientData;
  713.     TkWindow *winPtr;
  714.     register WmInfo *wmPtr;
  715.     int c;
  716.     size_t length;
  717.  
  718.     if (argc < 2) {
  719.     wrongNumArgs:
  720.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  721.         argv[0], " option window ?arg ...?\"", (char *) NULL);
  722.     return TCL_ERROR;
  723.     }
  724.     c = argv[1][0];
  725.     length = strlen(argv[1]);
  726.     if ((c == 't') && (strncmp(argv[1], "tracing", length) == 0)
  727.         && (length >= 3)) {
  728.     if ((argc != 2) && (argc != 3)) {
  729.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  730.             argv[0], " tracing ?boolean?\"", (char *) NULL);
  731.         return TCL_ERROR;
  732.     }
  733.     if (argc == 2) {
  734.         interp->result = (wmTracing) ? "on" : "off";
  735.         return TCL_OK;
  736.     }
  737.     return Tcl_GetBoolean(interp, argv[2], &wmTracing);
  738.     }
  739.  
  740.     if (argc < 3) {
  741.     goto wrongNumArgs;
  742.     }
  743.     winPtr = (TkWindow *) Tk_NameToWindow(interp, argv[2], tkwin);
  744.     if (winPtr == NULL) {
  745.     return TCL_ERROR;
  746.     }
  747.     if (!(winPtr->flags & TK_TOP_LEVEL)) {
  748.     Tcl_AppendResult(interp, "window \"", winPtr->pathName,
  749.         "\" isn't a top-level window", (char *) NULL);
  750.     return TCL_ERROR;
  751.     }
  752.     wmPtr = winPtr->wmInfoPtr;
  753.     if ((c == 'a') && (strncmp(argv[1], "aspect", length) == 0)) {
  754.     int numer1, denom1, numer2, denom2;
  755.  
  756.     if ((argc != 3) && (argc != 7)) {
  757.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  758.             argv[0], " aspect window ?minNumer minDenom ",
  759.             "maxNumer maxDenom?\"", (char *) NULL);
  760.         return TCL_ERROR;
  761.     }
  762.     if (argc == 3) {
  763.         if (wmPtr->sizeHintsFlags & PAspect) {
  764.         sprintf(interp->result, "%d %d %d %d", wmPtr->minAspect.x,
  765.             wmPtr->minAspect.y, wmPtr->maxAspect.x,
  766.             wmPtr->maxAspect.y);
  767.         }
  768.         return TCL_OK;
  769.     }
  770.     if (*argv[3] == '\0') {
  771.         wmPtr->sizeHintsFlags &= ~PAspect;
  772.     } else {
  773.         if ((Tcl_GetInt(interp, argv[3], &numer1) != TCL_OK)
  774.             || (Tcl_GetInt(interp, argv[4], &denom1) != TCL_OK)
  775.             || (Tcl_GetInt(interp, argv[5], &numer2) != TCL_OK)
  776.             || (Tcl_GetInt(interp, argv[6], &denom2) != TCL_OK)) {
  777.         return TCL_ERROR;
  778.         }
  779.         if ((numer1 <= 0) || (denom1 <= 0) || (numer2 <= 0) ||
  780.             (denom2 <= 0)) {
  781.         interp->result = "aspect number can't be <= 0";
  782.         return TCL_ERROR;
  783.         }
  784.         wmPtr->minAspect.x = numer1;
  785.         wmPtr->minAspect.y = denom1;
  786.         wmPtr->maxAspect.x = numer2;
  787.         wmPtr->maxAspect.y = denom2;
  788.         wmPtr->sizeHintsFlags |= PAspect;
  789.     }
  790.     wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
  791.     goto updateGeom;
  792.     } else if ((c == 'c') && (strncmp(argv[1], "client", length) == 0)
  793.         && (length >= 2)) {
  794.     if ((argc != 3) && (argc != 4)) {
  795.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  796.             argv[0], " client window ?name?\"",
  797.             (char *) NULL);
  798.         return TCL_ERROR;
  799.     }
  800.     if (argc == 3) {
  801.         if (wmPtr->clientMachine != NULL) {
  802.         interp->result = wmPtr->clientMachine;
  803.         }
  804.         return TCL_OK;
  805.     }
  806.     if (argv[3][0] == 0) {
  807.         if (wmPtr->clientMachine != NULL) {
  808.         ckfree((char *) wmPtr->clientMachine);
  809.         wmPtr->clientMachine = NULL;
  810.         if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  811.             XDeleteProperty(winPtr->display, winPtr->window,
  812.                 Tk_InternAtom((Tk_Window) winPtr,
  813.                 "WM_CLIENT_MACHINE"));
  814.         }
  815.         }
  816.         return TCL_OK;
  817.     }
  818.     if (wmPtr->clientMachine != NULL) {
  819.         ckfree((char *) wmPtr->clientMachine);
  820.     }
  821.     wmPtr->clientMachine = (char *)
  822.         ckalloc((unsigned) (strlen(argv[3]) + 1));
  823.     strcpy(wmPtr->clientMachine, argv[3]);
  824.     if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  825.         XTextProperty textProp;
  826.         if (XStringListToTextProperty(&wmPtr->clientMachine, 1, &textProp)
  827.             != 0) {
  828.         XSetWMClientMachine(winPtr->display, winPtr->window,
  829.             &textProp);
  830.         XFree((char *) textProp.value);
  831.         }
  832.     }
  833.     } else if ((c == 'c') && (strncmp(argv[1], "colormapwindows", length) == 0)
  834.         && (length >= 3)) {
  835.     Window *cmapList;
  836.     TkWindow *winPtr2;
  837.     int count, i, windowArgc, gotToplevel;
  838.     char buffer[20], **windowArgv;
  839.  
  840.     if ((argc != 3) && (argc != 4)) {
  841.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  842.             argv[0], " colormapwindows window ?windowList?\"",
  843.             (char *) NULL);
  844.         return TCL_ERROR;
  845.     }
  846.     if (argc == 3) {
  847.         Tk_MakeWindowExist((Tk_Window) winPtr);
  848.         if (XGetWMColormapWindows(winPtr->display, winPtr->window,
  849.             &cmapList, &count) == 0) {
  850.         return TCL_OK;
  851.         }
  852. #ifdef STk_CODE
  853.         Tcl_AppendResult(interp, "(", NULL);
  854. #endif
  855.         for (i = 0; i < count; i++) {
  856.         if ((i == (count-1))
  857.             && (wmPtr->flags & WM_ADDED_TOPLEVEL_COLORMAP)) {
  858.             break;
  859.         }
  860.             winPtr2  = (TkWindow *) Tk_IdToWindow(winPtr->display,
  861.             cmapList[i]);
  862.         if (winPtr2 == NULL) {
  863. #ifdef STk_CODE
  864.             sprintf(buffer, "#x%lx", cmapList[i]);
  865. #else
  866.             sprintf(buffer, "0x%lx", cmapList[i]);
  867. #endif
  868.             Tcl_AppendElement(interp, buffer);
  869.         } else {
  870.             Tcl_AppendElement(interp, winPtr2->pathName);
  871.         }
  872.         }
  873. #ifdef STk_CODE
  874.         Tcl_AppendResult(interp, ")", NULL);
  875. #endif
  876.         XFree((char *) cmapList);
  877.         return TCL_OK;
  878.     }
  879.     if (Tcl_SplitList(interp, argv[3], &windowArgc, &windowArgv)
  880.         != TCL_OK) {
  881.         return TCL_ERROR;
  882.     }
  883.     cmapList = (Window *) ckalloc((unsigned)
  884.         ((windowArgc+1)*sizeof(Window)));
  885.     gotToplevel = 0;
  886.     for (i = 0; i < windowArgc; i++) {
  887.         winPtr2 = (TkWindow *) Tk_NameToWindow(interp, windowArgv[i],
  888.             tkwin);
  889.         if (winPtr2 == NULL) {
  890.         ckfree((char *) cmapList);
  891.         ckfree((char *) windowArgv);
  892.         return TCL_ERROR;
  893.         }
  894.         if (winPtr2 == winPtr) {
  895.         gotToplevel = 1;
  896.         }
  897.         if (winPtr2->window == None) {
  898.         Tk_MakeWindowExist((Tk_Window) winPtr2);
  899.         }
  900.         cmapList[i] = winPtr2->window;
  901.     }
  902.     if (!gotToplevel) {
  903.         wmPtr->flags |= WM_ADDED_TOPLEVEL_COLORMAP;
  904.         cmapList[windowArgc] = winPtr->window;
  905.         windowArgc++;
  906.     } else {
  907.         wmPtr->flags &= ~WM_ADDED_TOPLEVEL_COLORMAP;
  908.     }
  909.     wmPtr->flags |= WM_COLORMAPS_EXPLICIT;
  910.     XSetWMColormapWindows(winPtr->display, winPtr->window, cmapList,
  911.         windowArgc);
  912.     ckfree((char *) cmapList);
  913.     ckfree((char *) windowArgv);
  914.     return TCL_OK;
  915.     } else if ((c == 'c') && (strncmp(argv[1], "command", length) == 0)
  916.         && (length >= 3)) {
  917.     int cmdArgc;
  918.     char **cmdArgv;
  919.  
  920.     if ((argc != 3) && (argc != 4)) {
  921.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  922.             argv[0], " command window ?value?\"",
  923.             (char *) NULL);
  924.         return TCL_ERROR;
  925.     }
  926.     if (argc == 3) {
  927.         if (wmPtr->cmdArgv != NULL) {
  928.         interp->result = Tcl_Merge(wmPtr->cmdArgc, wmPtr->cmdArgv);
  929.         interp->freeProc = TCL_DYNAMIC;
  930.         }
  931.         return TCL_OK;
  932.     }
  933.     if (argv[3][0] == 0) {
  934.         if (wmPtr->cmdArgv != NULL) {
  935.         ckfree((char *) wmPtr->cmdArgv);
  936.         wmPtr->cmdArgv = NULL;
  937.         if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  938.             XDeleteProperty(winPtr->display, winPtr->window,
  939.                 Tk_InternAtom((Tk_Window) winPtr, "WM_COMMAND"));
  940.         }
  941.         }
  942.         return TCL_OK;
  943.     }
  944.     if (Tcl_SplitList(interp, argv[3], &cmdArgc, &cmdArgv) != TCL_OK) {
  945.         return TCL_ERROR;
  946.     }
  947.     if (wmPtr->cmdArgv != NULL) {
  948.         ckfree((char *) wmPtr->cmdArgv);
  949.     }
  950.     wmPtr->cmdArgc = cmdArgc;
  951.     wmPtr->cmdArgv = cmdArgv;
  952.     if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  953.         XSetCommand(winPtr->display, winPtr->window, cmdArgv, cmdArgc);
  954.     }
  955.     } else if ((c == 'd') && (strncmp(argv[1], "deiconify", length) == 0)) {
  956.     if (argc != 3) {
  957.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  958.             argv[0], " deiconify window\"", (char *) NULL);
  959.         return TCL_ERROR;
  960.     }
  961.     if (wmPtr->iconFor != NULL) {
  962.         Tcl_AppendResult(interp, "can't deiconify ", argv[2],
  963.             ": it is an icon for ", winPtr->pathName, (char *) NULL);
  964.         return TCL_ERROR;
  965.     }
  966.     wmPtr->hints.initial_state = NormalState;
  967.     wmPtr->withdrawn = 0;
  968.     if (wmPtr->flags & WM_NEVER_MAPPED) {
  969.         return TCL_OK;
  970.     }
  971.     UpdateHints(winPtr);
  972.     Tk_MapWindow((Tk_Window) winPtr);
  973.     } else if ((c == 'f') && (strncmp(argv[1], "focusmodel", length) == 0)
  974.         && (length >= 2)) {
  975.     if ((argc != 3) && (argc != 4)) {
  976.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  977.             argv[0], " focusmodel window ?active|passive?\"",
  978.             (char *) NULL);
  979.         return TCL_ERROR;
  980.     }
  981.     if (argc == 3) {
  982. #ifdef STk_CODE
  983.         interp->result = wmPtr->hints.input ? "\"passive\"" : "\"active\"";
  984. #else
  985.         interp->result = wmPtr->hints.input ? "passive" : "active";
  986. #endif
  987.         return TCL_OK;
  988.     }
  989.     c = argv[3][0];
  990.     length = strlen(argv[3]);
  991.     if ((c == 'a') && (strncmp(argv[3], "active", length) == 0)) {
  992.         wmPtr->hints.input = False;
  993.     } else if ((c == 'p') && (strncmp(argv[3], "passive", length) == 0)) {
  994.         wmPtr->hints.input = True;
  995.     } else {
  996.         Tcl_AppendResult(interp, "bad argument \"", argv[3],
  997.             "\": must be active or passive", (char *) NULL);
  998.         return TCL_ERROR;
  999.     }
  1000.     UpdateHints(winPtr);
  1001.     } else if ((c == 'f') && (strncmp(argv[1], "frame", length) == 0)
  1002.         && (length >= 2)) {
  1003.     Window window;
  1004.  
  1005.     if (argc != 3) {
  1006.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1007.             argv[0], " frame window\"", (char *) NULL);
  1008.         return TCL_ERROR;
  1009.     }
  1010.     window = wmPtr->reparent;
  1011.     if (window == None) {
  1012.         window = Tk_WindowId((Tk_Window) winPtr);
  1013.     }
  1014. #ifdef STk_CODE
  1015.     sprintf(interp->result, "#x%x", (unsigned int) window);
  1016. #else
  1017.     sprintf(interp->result, "0x%x", (unsigned int) window);
  1018. #endif
  1019.     } else if ((c == 'g') && (strncmp(argv[1], "geometry", length) == 0)
  1020.         && (length >= 2)) {
  1021.     char xSign, ySign;
  1022.     int width, height;
  1023.  
  1024.     if ((argc != 3) && (argc != 4)) {
  1025.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1026.             argv[0], " geometry window ?newGeometry?\"",
  1027.             (char *) NULL);
  1028.         return TCL_ERROR;
  1029.     }
  1030.     if (argc == 3) {
  1031.         xSign = (wmPtr->flags & WM_NEGATIVE_X) ? '-' : '+';
  1032.         ySign = (wmPtr->flags & WM_NEGATIVE_Y) ? '-' : '+';
  1033.         if (wmPtr->gridWin != NULL) {
  1034.         width = wmPtr->reqGridWidth + (winPtr->changes.width
  1035.             - winPtr->reqWidth)/wmPtr->widthInc;
  1036.         height = wmPtr->reqGridHeight + (winPtr->changes.height
  1037.             - winPtr->reqHeight)/wmPtr->heightInc;
  1038.         } else {
  1039.         width = winPtr->changes.width;
  1040.         height = winPtr->changes.height;
  1041.         }
  1042. #ifdef STk_CODE
  1043.         sprintf(interp->result, "\"%dx%d%c%d%c%d\"", width, height,
  1044. #else
  1045.         sprintf(interp->result, "%dx%d%c%d%c%d", width, height,
  1046. #endif
  1047.             xSign, wmPtr->x, ySign, wmPtr->y);
  1048.         return TCL_OK;
  1049.     }
  1050.     if (*argv[3] == '\0') {
  1051.         wmPtr->width = -1;
  1052.         wmPtr->height = -1;
  1053.         goto updateGeom;
  1054.     }
  1055.     return ParseGeometry(interp, argv[3], winPtr);
  1056.     } else if ((c == 'g') && (strncmp(argv[1], "grid", length) == 0)
  1057.         && (length >= 3)) {
  1058.     int reqWidth, reqHeight, widthInc, heightInc;
  1059.  
  1060.     if ((argc != 3) && (argc != 7)) {
  1061.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1062.             argv[0], " grid window ?baseWidth baseHeight ",
  1063.             "widthInc heightInc?\"", (char *) NULL);
  1064.         return TCL_ERROR;
  1065.     }
  1066.     if (argc == 3) {
  1067.         if (wmPtr->sizeHintsFlags & PBaseSize) {
  1068.         sprintf(interp->result, "%d %d %d %d", wmPtr->reqGridWidth,
  1069.             wmPtr->reqGridHeight, wmPtr->widthInc,
  1070.             wmPtr->heightInc);
  1071.         }
  1072. #ifdef STk_CODE
  1073.         else
  1074.           interp->result = "#f";
  1075. #endif
  1076.         return TCL_OK;
  1077.     }
  1078. #ifdef STk_CODE
  1079.     if (*argv[3] == '\0' || strcmp(argv[3], "#f") == 0) {
  1080. #else
  1081.     if (*argv[3] == '\0') {
  1082. #endif
  1083.         /*
  1084.          * Turn off gridding and reset the width and height
  1085.          * to make sense as ungridded numbers.
  1086.          */
  1087.  
  1088.         wmPtr->sizeHintsFlags &= ~(PBaseSize|PResizeInc);
  1089.         if (wmPtr->width != -1) {
  1090.         wmPtr->width = winPtr->reqWidth + (wmPtr->width
  1091.             - wmPtr->reqGridWidth)*wmPtr->widthInc;
  1092.         wmPtr->height = winPtr->reqHeight + (wmPtr->height
  1093.             - wmPtr->reqGridHeight)*wmPtr->heightInc;
  1094.         }
  1095.         wmPtr->widthInc = 1;
  1096.         wmPtr->heightInc = 1;
  1097.     } else {
  1098.         if ((Tcl_GetInt(interp, argv[3], &reqWidth) != TCL_OK)
  1099.             || (Tcl_GetInt(interp, argv[4], &reqHeight) != TCL_OK)
  1100.             || (Tcl_GetInt(interp, argv[5], &widthInc) != TCL_OK)
  1101.             || (Tcl_GetInt(interp, argv[6], &heightInc) != TCL_OK)) {
  1102.         return TCL_ERROR;
  1103.         }
  1104.         if (reqWidth < 0) {
  1105.         interp->result = "baseWidth can't be < 0";
  1106.         return TCL_ERROR;
  1107.         }
  1108.         if (reqHeight < 0) {
  1109.         interp->result = "baseHeight can't be < 0";
  1110.         return TCL_ERROR;
  1111.         }
  1112.         if (widthInc < 0) {
  1113.         interp->result = "widthInc can't be < 0";
  1114.         return TCL_ERROR;
  1115.         }
  1116.         if (heightInc < 0) {
  1117.         interp->result = "heightInc can't be < 0";
  1118.         return TCL_ERROR;
  1119.         }
  1120.         Tk_SetGrid((Tk_Window) winPtr, reqWidth, reqHeight, widthInc,
  1121.             heightInc);
  1122.     }
  1123.     wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
  1124.     goto updateGeom;
  1125.     } else if ((c == 'g') && (strncmp(argv[1], "group", length) == 0)
  1126.         && (length >= 3)) {
  1127.     Tk_Window tkwin2;
  1128.  
  1129.     if ((argc != 3) && (argc != 4)) {
  1130.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1131.             argv[0], " group window ?pathName?\"",
  1132.             (char *) NULL);
  1133.         return TCL_ERROR;
  1134.     }
  1135.     if (argc == 3) {
  1136.         if (wmPtr->hints.flags & WindowGroupHint) {
  1137.         interp->result = wmPtr->leaderName;
  1138.         }
  1139. #ifdef STk_CODE
  1140.         else
  1141.           interp->result = "#f";
  1142.         STk_sharp_dot_result(interp, interp->result);
  1143. #endif
  1144.         return TCL_OK;
  1145.     }
  1146. #ifdef STk_CODE
  1147.     if (*argv[3] == '\0' || strcmp(argv[3], "#f") == 0) {
  1148. #else
  1149.     if (*argv[3] == '\0') {
  1150. #endif
  1151.         wmPtr->hints.flags &= ~WindowGroupHint;
  1152.         wmPtr->leaderName = NULL;
  1153.     } else {
  1154.         tkwin2 = Tk_NameToWindow(interp, argv[3], tkwin);
  1155.         if (tkwin2 == NULL) {
  1156.         return TCL_ERROR;
  1157.         }
  1158.         Tk_MakeWindowExist(tkwin2);
  1159.         wmPtr->hints.window_group = Tk_WindowId(tkwin2);
  1160.         wmPtr->hints.flags |= WindowGroupHint;
  1161.         wmPtr->leaderName = Tk_PathName(tkwin2);
  1162.     }
  1163.     UpdateHints(winPtr);
  1164.     } else if ((c == 'i') && (strncmp(argv[1], "iconbitmap", length) == 0)
  1165.         && (length >= 5)) {
  1166.     Pixmap pixmap;
  1167.  
  1168.     if ((argc != 3) && (argc != 4)) {
  1169.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1170.             argv[0], " iconbitmap window ?bitmap?\"",
  1171.             (char *) NULL);
  1172.         return TCL_ERROR;
  1173.     }
  1174.     if (argc == 3) {
  1175.         if (wmPtr->hints.flags & IconPixmapHint) {
  1176.         interp->result = Tk_NameOfBitmap(winPtr->display,
  1177.             wmPtr->hints.icon_pixmap);
  1178. #ifdef STk_CODE
  1179.         STk_stringify_result(interp, interp->result);
  1180.         }
  1181.         else {
  1182.           interp->result = "#f";
  1183. #endif
  1184.         }
  1185.         return TCL_OK;
  1186.     }
  1187. #ifdef STk_CODE
  1188.     if (*argv[3] == '\0' || strcmp(argv[3], "#f") == 0) {
  1189. #else
  1190.     if (*argv[3] == '\0') {
  1191. #endif
  1192.         if (wmPtr->hints.icon_pixmap != None) {
  1193.         Tk_FreeBitmap(winPtr->display, wmPtr->hints.icon_pixmap);
  1194.         wmPtr->hints.icon_pixmap = None;
  1195.         }
  1196.         wmPtr->hints.flags &= ~IconPixmapHint;
  1197.     } else {
  1198.         pixmap = Tk_GetBitmap(interp, (Tk_Window) winPtr,
  1199.             Tk_GetUid(argv[3]));
  1200.         if (pixmap == None) {
  1201.         return TCL_ERROR;
  1202.         }
  1203.         wmPtr->hints.icon_pixmap = pixmap;
  1204.         wmPtr->hints.flags |= IconPixmapHint;
  1205.     }
  1206.     UpdateHints(winPtr);
  1207.     } else if ((c == 'i') && (strncmp(argv[1], "iconify", length) == 0)
  1208.         && (length >= 5)) {
  1209.     if (argc != 3) {
  1210.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1211.             argv[0], " iconify window\"", (char *) NULL);
  1212.         return TCL_ERROR;
  1213.     }
  1214.     if (Tk_Attributes((Tk_Window) winPtr)->override_redirect) {
  1215.         Tcl_AppendResult(interp, "can't iconify \"", winPtr->pathName,
  1216.             "\": override-redirect flag is set", (char *) NULL);
  1217.         return TCL_ERROR;
  1218.     }
  1219.     if (wmPtr->master != None) {
  1220.         Tcl_AppendResult(interp, "can't iconify \"", winPtr->pathName,
  1221.             "\": it is a transient", (char *) NULL);
  1222.         return TCL_ERROR;
  1223.     }
  1224.     if (wmPtr->iconFor != NULL) {
  1225.         Tcl_AppendResult(interp, "can't iconify ", argv[2],
  1226.             ": it is an icon for ", winPtr->pathName, (char *) NULL);
  1227.         return TCL_ERROR;
  1228.     }
  1229.     wmPtr->hints.initial_state = IconicState;
  1230.     if (wmPtr->flags & WM_NEVER_MAPPED) {
  1231.         return TCL_OK;
  1232.     }
  1233.     if (wmPtr->withdrawn) {
  1234.         UpdateHints(winPtr);
  1235.         Tk_MapWindow((Tk_Window) winPtr);
  1236.         wmPtr->withdrawn = 0;
  1237.     } else {
  1238.         if (XIconifyWindow(winPtr->display, winPtr->window,
  1239.         winPtr->screenNum) == 0) {
  1240.         interp->result =
  1241.             "couldn't send iconify message to window manager";
  1242.         return TCL_ERROR;
  1243.         }
  1244.         WaitForMapNotify(winPtr, 0);
  1245.     }
  1246.     } else if ((c == 'i') && (strncmp(argv[1], "iconmask", length) == 0)
  1247.         && (length >= 5)) {
  1248.     Pixmap pixmap;
  1249.  
  1250.     if ((argc != 3) && (argc != 4)) {
  1251.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1252.             argv[0], " iconmask window ?bitmap?\"",
  1253.             (char *) NULL);
  1254.         return TCL_ERROR;
  1255.     }
  1256.     if (argc == 3) {
  1257.         if (wmPtr->hints.flags & IconMaskHint) {
  1258.         interp->result = Tk_NameOfBitmap(winPtr->display,
  1259.             wmPtr->hints.icon_mask);
  1260.         }
  1261. #ifdef STk_CODE
  1262.         STk_stringify_result(interp, interp->result);
  1263. #endif
  1264.         return TCL_OK;
  1265.     }
  1266. #ifdef STk_CODE
  1267.     if (*argv[3] == '\0' || strcmp(argv[3], "#f") == 0) {
  1268. #else
  1269.     if (*argv[3] == '\0') {
  1270. #endif
  1271.         if (wmPtr->hints.icon_mask != None) {
  1272.         Tk_FreeBitmap(winPtr->display, wmPtr->hints.icon_mask);
  1273.         }
  1274.         wmPtr->hints.flags &= ~IconMaskHint;
  1275.     } else {
  1276.         pixmap = Tk_GetBitmap(interp, tkwin, Tk_GetUid(argv[3]));
  1277.         if (pixmap == None) {
  1278.         return TCL_ERROR;
  1279.         }
  1280.         wmPtr->hints.icon_mask = pixmap;
  1281.         wmPtr->hints.flags |= IconMaskHint;
  1282.     }
  1283.     UpdateHints(winPtr);
  1284.     } else if ((c == 'i') && (strncmp(argv[1], "iconname", length) == 0)
  1285.         && (length >= 5)) {
  1286.     if (argc > 4) {
  1287.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1288.             argv[0], " iconname window ?newName?\"", (char *) NULL);
  1289.         return TCL_ERROR;
  1290.     }
  1291.     if (argc == 3) {
  1292.         interp->result = (wmPtr->iconName != NULL) ? wmPtr->iconName : "";
  1293. #ifdef STk_CODE
  1294.         STk_stringify_result(interp, interp->result);
  1295. #endif
  1296.         return TCL_OK;
  1297.     } else {
  1298.         wmPtr->iconName = Tk_GetUid(argv[3]);
  1299.         if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  1300.         XSetIconName(winPtr->display, winPtr->window, wmPtr->iconName);
  1301.         }
  1302.     }
  1303.     } else if ((c == 'i') && (strncmp(argv[1], "iconposition", length) == 0)
  1304.         && (length >= 5)) {
  1305.     int x, y;
  1306.  
  1307.     if ((argc != 3) && (argc != 5)) {
  1308.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1309.             argv[0], " iconposition window ?x y?\"",
  1310.             (char *) NULL);
  1311.         return TCL_ERROR;
  1312.     }
  1313.     if (argc == 3) {
  1314.         if (wmPtr->hints.flags & IconPositionHint) {
  1315.         sprintf(interp->result, "%d %d", wmPtr->hints.icon_x,
  1316.             wmPtr->hints.icon_y);
  1317.         }
  1318. #ifdef STk_CODE
  1319.         else 
  1320.           interp->result = "#f";
  1321. #endif
  1322.         return TCL_OK;
  1323.     }
  1324. #ifdef STk_CODE
  1325.     if (*argv[3] == '\0' || strcmp(argv[3], "#f") == 0) {
  1326. #else
  1327.     if (*argv[3] == '\0') {
  1328. #endif
  1329.         wmPtr->hints.flags &= ~IconPositionHint;
  1330.     } else {
  1331.         if ((Tcl_GetInt(interp, argv[3], &x) != TCL_OK)
  1332.             || (Tcl_GetInt(interp, argv[4], &y) != TCL_OK)){
  1333.         return TCL_ERROR;
  1334.         }
  1335.         wmPtr->hints.icon_x = x;
  1336.         wmPtr->hints.icon_y = y;
  1337.         wmPtr->hints.flags |= IconPositionHint;
  1338.     }
  1339.     UpdateHints(winPtr);
  1340.     } else if ((c == 'i') && (strncmp(argv[1], "iconwindow", length) == 0)
  1341.         && (length >= 5)) {
  1342.     Tk_Window tkwin2;
  1343.     WmInfo *wmPtr2;
  1344.     XSetWindowAttributes atts;
  1345.  
  1346.     if ((argc != 3) && (argc != 4)) {
  1347.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1348.             argv[0], " iconwindow window ?pathName?\"",
  1349.             (char *) NULL);
  1350.         return TCL_ERROR;
  1351.     }
  1352.     if (argc == 3) {
  1353.         if (wmPtr->icon != NULL) {
  1354.         interp->result = Tk_PathName(wmPtr->icon);
  1355.         }
  1356. #ifdef STk_CODE
  1357.         else
  1358.           interp->result = "#f";
  1359.         STk_sharp_dot_result(interp, interp->result);
  1360. #endif
  1361.         return TCL_OK;
  1362.     }
  1363. #ifdef STk_CODE
  1364.     if (*argv[3] == '\0' || strcmp(argv[3], "#f") == 0) {
  1365. #else
  1366.     if (*argv[3] == '\0') {
  1367. #endif
  1368.         wmPtr->hints.flags &= ~IconWindowHint;
  1369.         if (wmPtr->icon != NULL) {
  1370.         /*
  1371.          * Remove the icon window relationship.  In principle we
  1372.          * should also re-enable button events for the window, but
  1373.          * this doesn't work in general because the window manager
  1374.          * is probably selecting on them (we'll get an error if
  1375.          * we try to re-enable the events).  So, just leave the
  1376.          * icon window event-challenged;  the user will have to
  1377.          * recreate it if they want button events.
  1378.          */
  1379.  
  1380.         wmPtr2 = ((TkWindow *) wmPtr->icon)->wmInfoPtr;
  1381.         wmPtr2->iconFor = NULL;
  1382.         wmPtr2->withdrawn = 1;
  1383.         wmPtr2->hints.initial_state = WithdrawnState;
  1384.         }
  1385.         wmPtr->icon = NULL;
  1386.     } else {
  1387.         tkwin2 = Tk_NameToWindow(interp, argv[3], tkwin);
  1388.         if (tkwin2 == NULL) {
  1389.         return TCL_ERROR;
  1390.         }
  1391.         if (!Tk_IsTopLevel(tkwin2)) {
  1392.         Tcl_AppendResult(interp, "can't use ", argv[3],
  1393.             " as icon window: not at top level", (char *) NULL);
  1394.         return TCL_ERROR;
  1395.         }
  1396.         wmPtr2 = ((TkWindow *) tkwin2)->wmInfoPtr;
  1397.         if (wmPtr2->iconFor != NULL) {
  1398.         Tcl_AppendResult(interp, argv[3], " is already an icon for ",
  1399.             Tk_PathName(wmPtr2->iconFor), (char *) NULL);
  1400.         return TCL_ERROR;
  1401.         }
  1402.         if (wmPtr->icon != NULL) {
  1403.         WmInfo *wmPtr3 = ((TkWindow *) wmPtr->icon)->wmInfoPtr;
  1404.         wmPtr3->iconFor = NULL;
  1405.         wmPtr3->withdrawn = 1;
  1406.  
  1407.         /*
  1408.          * Let the window use button events again.
  1409.          */
  1410.  
  1411.         atts.event_mask = Tk_Attributes(wmPtr->icon)->event_mask
  1412.             | ButtonPressMask;
  1413.         Tk_ChangeWindowAttributes(wmPtr->icon, CWEventMask, &atts);
  1414.         }
  1415.  
  1416.         /*
  1417.          * Disable button events in the icon window:  some window
  1418.          * managers (like olvwm) want to get the events themselves,
  1419.          * but X only allows one application at a time to receive
  1420.          * button events for a window.
  1421.          */
  1422.  
  1423.         atts.event_mask = Tk_Attributes(tkwin2)->event_mask
  1424.             & ~ButtonPressMask;
  1425.         Tk_ChangeWindowAttributes(tkwin2, CWEventMask, &atts);
  1426.         Tk_MakeWindowExist(tkwin2);
  1427.         wmPtr->hints.icon_window = Tk_WindowId(tkwin2);
  1428.         wmPtr->hints.flags |= IconWindowHint;
  1429.         wmPtr->icon = tkwin2;
  1430.         wmPtr2->iconFor = (Tk_Window) winPtr;
  1431.         if (!wmPtr2->withdrawn && !(wmPtr2->flags & WM_NEVER_MAPPED)) {
  1432.         wmPtr2->withdrawn = 0;
  1433.         if (XWithdrawWindow(Tk_Display(tkwin2), Tk_WindowId(tkwin2),
  1434.             Tk_ScreenNumber(tkwin2)) == 0) {
  1435.             interp->result =
  1436.                 "couldn't send withdraw message to window manager";
  1437.             return TCL_ERROR;
  1438.         }
  1439.         WaitForMapNotify((TkWindow *) tkwin2, 0);
  1440.         }
  1441.     }
  1442.     UpdateHints(winPtr);
  1443.     } else if ((c == 'm') && (strncmp(argv[1], "maxsize", length) == 0)
  1444.         && (length >= 2)) {
  1445.     int width, height;
  1446.     if ((argc != 3) && (argc != 5)) {
  1447.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1448.             argv[0], " maxsize window ?width height?\"", (char *) NULL);
  1449.         return TCL_ERROR;
  1450.     }
  1451.     if (argc == 3) {
  1452.         GetMaxSize(wmPtr, &width, &height);
  1453.         sprintf(interp->result, "%d %d", width, height);
  1454.         return TCL_OK;
  1455.     }
  1456.     if ((Tcl_GetInt(interp, argv[3], &width) != TCL_OK)
  1457.         || (Tcl_GetInt(interp, argv[4], &height) != TCL_OK)) {
  1458.         return TCL_ERROR;
  1459.     }
  1460.     wmPtr->maxWidth = width;
  1461.     wmPtr->maxHeight = height;
  1462.     wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
  1463.     goto updateGeom;
  1464.     } else if ((c == 'm') && (strncmp(argv[1], "minsize", length) == 0)
  1465.         && (length >= 2)) {
  1466.     int width, height;
  1467.     if ((argc != 3) && (argc != 5)) {
  1468.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1469.             argv[0], " minsize window ?width height?\"", (char *) NULL);
  1470.         return TCL_ERROR;
  1471.     }
  1472.     if (argc == 3) {
  1473.         sprintf(interp->result, "%d %d", wmPtr->minWidth,
  1474.             wmPtr->minHeight);
  1475.         return TCL_OK;
  1476.     }
  1477.     if ((Tcl_GetInt(interp, argv[3], &width) != TCL_OK)
  1478.         || (Tcl_GetInt(interp, argv[4], &height) != TCL_OK)) {
  1479.         return TCL_ERROR;
  1480.     }
  1481.     wmPtr->minWidth = width;
  1482.     wmPtr->minHeight = height;
  1483.     wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
  1484.     goto updateGeom;
  1485.     } else if ((c == 'o')
  1486.         && (strncmp(argv[1], "overrideredirect", length) == 0)) {
  1487.     int boolean;
  1488.     XSetWindowAttributes atts;
  1489.  
  1490.     if ((argc != 3) && (argc != 4)) {
  1491.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1492.             argv[0], " overrideredirect window ?boolean?\"",
  1493.             (char *) NULL);
  1494.         return TCL_ERROR;
  1495.     }
  1496.     if (argc == 3) {
  1497. #ifdef STk_CODE
  1498.         interp->result = (Tk_Attributes((Tk_Window) winPtr)->override_redirect)
  1499.                           ? "#t" : "#f";
  1500. #else
  1501.         if (Tk_Attributes((Tk_Window) winPtr)->override_redirect) {
  1502.         interp->result = "1";
  1503.         } else {
  1504.         interp->result = "0";
  1505.         }
  1506. #endif
  1507.         return TCL_OK;
  1508.     }
  1509.     if (Tcl_GetBoolean(interp, argv[3], &boolean) != TCL_OK) {
  1510.         return TCL_ERROR;
  1511.     }
  1512.     atts.override_redirect = (boolean) ? True : False;
  1513.     Tk_ChangeWindowAttributes((Tk_Window) winPtr, CWOverrideRedirect,
  1514.         &atts);
  1515.     } else if ((c == 'p') && (strncmp(argv[1], "positionfrom", length) == 0)
  1516.         && (length >= 2)) {
  1517.     if ((argc != 3) && (argc != 4)) {
  1518.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1519.             argv[0], " positionfrom window ?user/program?\"",
  1520.             (char *) NULL);
  1521.         return TCL_ERROR;
  1522.     }
  1523.     if (argc == 3) {
  1524.         if (wmPtr->sizeHintsFlags & USPosition) {
  1525. #ifdef STk_CODE
  1526.         interp->result = "\"user\"";
  1527. #else
  1528.         interp->result = "user";
  1529. #endif
  1530.         } else if (wmPtr->sizeHintsFlags & PPosition) {
  1531. #ifdef STk_CODE
  1532.         interp->result = "\"program\"";
  1533.         } else {
  1534.             interp->result = "#f";
  1535. #else
  1536.         interp->result = "program";
  1537. #endif
  1538.         }
  1539.         return TCL_OK;
  1540.     }
  1541. #ifdef STk_CODE
  1542.     if (*argv[3] == '\0' || strcmp(argv[3], "#f") == 0) {
  1543. #else
  1544.     if (*argv[3] == '\0') {
  1545. #endif
  1546.         wmPtr->sizeHintsFlags &= ~(USPosition|PPosition);
  1547.     } else {
  1548.         c = argv[3][0];
  1549.         length = strlen(argv[3]);
  1550.         if ((c == 'u') && (strncmp(argv[3], "user", length) == 0)) {
  1551.         wmPtr->sizeHintsFlags &= ~PPosition;
  1552.         wmPtr->sizeHintsFlags |= USPosition;
  1553.         } else if ((c == 'p') && (strncmp(argv[3], "program", length) == 0)) {
  1554.         wmPtr->sizeHintsFlags &= ~USPosition;
  1555.         wmPtr->sizeHintsFlags |= PPosition;
  1556.         } else {
  1557.         Tcl_AppendResult(interp, "bad argument \"", argv[3],
  1558.             "\": must be program or user", (char *) NULL);
  1559.         return TCL_ERROR;
  1560.         }
  1561.     }
  1562.     wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
  1563.     goto updateGeom;
  1564.     } else if ((c == 'p') && (strncmp(argv[1], "protocol", length) == 0)
  1565.         && (length >= 2)) {
  1566.     register ProtocolHandler *protPtr, *prevPtr;
  1567.     Atom protocol;
  1568.     int cmdLength;
  1569.  
  1570.     if ((argc < 3) || (argc > 5)) {
  1571.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1572.             argv[0], " protocol window ?name? ?command?\"",
  1573.             (char *) NULL);
  1574.         return TCL_ERROR;
  1575.     }
  1576.     if (argc == 3) {
  1577.         /*
  1578.          * Return a list of all defined protocols for the window.
  1579.          */
  1580. #ifdef STk_CODE
  1581.         Tcl_AppendResult(interp, "(", NULL);
  1582.         for (protPtr = wmPtr->protPtr; protPtr; protPtr = protPtr->nextPtr) {
  1583.           Tcl_AppendResult(interp, " \"",
  1584.             Tk_GetAtomName((Tk_Window) winPtr, protPtr->protocol),
  1585.                    "\"", (char *)NULL);
  1586.         }
  1587.         Tcl_AppendResult(interp, ")", NULL);
  1588. #else
  1589.         for (protPtr = wmPtr->protPtr; protPtr != NULL;
  1590.             protPtr = protPtr->nextPtr) {
  1591.         Tcl_AppendElement(interp,
  1592.             Tk_GetAtomName((Tk_Window) winPtr, protPtr->protocol));
  1593.         }
  1594. #endif
  1595.         return TCL_OK;
  1596.     }
  1597.     protocol = Tk_InternAtom((Tk_Window) winPtr, argv[3]);
  1598.     if (argc == 4) {
  1599.         /*
  1600.          * Return the command to handle a given protocol.
  1601.          */
  1602.         for (protPtr = wmPtr->protPtr; protPtr != NULL;
  1603.             protPtr = protPtr->nextPtr) {
  1604.         if (protPtr->protocol == protocol) {
  1605. #ifdef STk_CODE
  1606.             STk_sharp_dot_result(interp, protPtr->command);
  1607. #else
  1608.             interp->result = protPtr->command;
  1609. #endif
  1610.             return TCL_OK;
  1611.         }
  1612.         }
  1613.         return TCL_OK;
  1614.     }
  1615.  
  1616.     /*
  1617.      * Delete any current protocol handler, then create a new
  1618.      * one with the specified command, unless the command is
  1619.      * empty.
  1620.      */
  1621.  
  1622.     for (protPtr = wmPtr->protPtr, prevPtr = NULL; protPtr != NULL;
  1623.         prevPtr = protPtr, protPtr = protPtr->nextPtr) {
  1624.         if (protPtr->protocol == protocol) {
  1625.         if (prevPtr == NULL) {
  1626.             wmPtr->protPtr = protPtr->nextPtr;
  1627.         } else {
  1628.             prevPtr->nextPtr = protPtr->nextPtr;
  1629.         }
  1630.         Tcl_EventuallyFree((ClientData) protPtr, TCL_DYNAMIC);
  1631.         break;
  1632.         }
  1633.     }
  1634.     cmdLength = strlen(argv[4]);
  1635.     if (cmdLength > 0) {
  1636. #ifdef STk_CODE
  1637.         SCM closure;
  1638.  
  1639.         if (!STk_valid_callback(argv[4], &closure)) {
  1640.             Tcl_AppendResult(interp, "bad closure specification \"",
  1641.                          argv[4], "\"", (char *) NULL);
  1642.         return TCL_ERROR;
  1643.         }
  1644.         if (closure)
  1645.           STk_add_callback(argv[2], "protocol", argv[3], closure);        
  1646. #endif
  1647.         protPtr = (ProtocolHandler *) ckalloc(HANDLER_SIZE(cmdLength));
  1648.         protPtr->protocol = protocol;
  1649.         protPtr->nextPtr = wmPtr->protPtr;
  1650.         wmPtr->protPtr = protPtr;
  1651.         protPtr->interp = interp;
  1652.         strcpy(protPtr->command, argv[4]);
  1653.     }
  1654. #ifdef STk_CODE
  1655.     else {
  1656.       /* Delete handler by setting it to #f */
  1657.       extern SCM STk_ntruth;
  1658.  
  1659.       STk_add_callback(argv[2], "protocol", argv[3], STk_ntruth);
  1660.     }
  1661. #endif
  1662.     if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  1663.         UpdateWmProtocols(wmPtr);
  1664.     }
  1665.     } else if ((c == 'r') && (strncmp(argv[1], "resizable", length) == 0)) {
  1666.     int width, height;
  1667.  
  1668.     if ((argc != 3) && (argc != 5)) {
  1669.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1670.             argv[0], " resizable window ?width height?\"",
  1671.             (char *) NULL);
  1672.         return TCL_ERROR;
  1673.     }
  1674.     if (argc == 3) {
  1675. #ifdef STk_CODE
  1676.         sprintf(interp->result, "(#%c #%c)",
  1677.             (wmPtr->flags  & WM_WIDTH_NOT_RESIZABLE) ? 'f' : 't',
  1678.             (wmPtr->flags  & WM_HEIGHT_NOT_RESIZABLE) ? 'f' : 't');
  1679. #else
  1680.         sprintf(interp->result, "%d %d",
  1681.             (wmPtr->flags  & WM_WIDTH_NOT_RESIZABLE) ? 0 : 1,
  1682.             (wmPtr->flags  & WM_HEIGHT_NOT_RESIZABLE) ? 0 : 1);
  1683. #endif
  1684.         return TCL_OK;
  1685.     }
  1686.     if ((Tcl_GetBoolean(interp, argv[3], &width) != TCL_OK)
  1687.         || (Tcl_GetBoolean(interp, argv[4], &height) != TCL_OK)) {
  1688.         return TCL_ERROR;
  1689.     }
  1690.     if (width) {
  1691.         wmPtr->flags &= ~WM_WIDTH_NOT_RESIZABLE;
  1692.     } else {
  1693.         wmPtr->flags |= WM_WIDTH_NOT_RESIZABLE;
  1694.     }
  1695.     if (height) {
  1696.         wmPtr->flags &= ~WM_HEIGHT_NOT_RESIZABLE;
  1697.     } else {
  1698.         wmPtr->flags |= WM_HEIGHT_NOT_RESIZABLE;
  1699.     }
  1700.     wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
  1701.     goto updateGeom;
  1702.     } else if ((c == 's') && (strncmp(argv[1], "sizefrom", length) == 0)
  1703.         && (length >= 2)) {
  1704.     if ((argc != 3) && (argc != 4)) {
  1705.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1706.             argv[0], " sizefrom window ?user|program?\"",
  1707.             (char *) NULL);
  1708.         return TCL_ERROR;
  1709.     }
  1710.     if (argc == 3) {
  1711.         if (wmPtr->sizeHintsFlags & USSize) {
  1712. #ifdef STk_CODE
  1713.         interp->result = "\"user\"";
  1714. #else
  1715.         interp->result = "user";
  1716. #endif
  1717.         } else if (wmPtr->sizeHintsFlags & PSize) {
  1718. #ifdef STk_CODE
  1719.         interp->result = "\"program\"";
  1720.         }
  1721.         else {
  1722.             interp->result = "#f";
  1723. #else
  1724.         interp->result = "program";
  1725. #endif
  1726.         }
  1727.         return TCL_OK;
  1728.     }
  1729. #ifdef STk_CODE
  1730.     if (*argv[3] == '\0' || strcmp(argv[3], "#f") == 0) {
  1731. #else
  1732.     if (*argv[3] == '\0') {
  1733. #endif
  1734.         wmPtr->sizeHintsFlags &= ~(USSize|PSize);
  1735.     } else {
  1736.         c = argv[3][0];
  1737.         length = strlen(argv[3]);
  1738.         if ((c == 'u') && (strncmp(argv[3], "user", length) == 0)) {
  1739.         wmPtr->sizeHintsFlags &= ~PSize;
  1740.         wmPtr->sizeHintsFlags |= USSize;
  1741.         } else if ((c == 'p')
  1742.             && (strncmp(argv[3], "program", length) == 0)) {
  1743.         wmPtr->sizeHintsFlags &= ~USSize;
  1744.         wmPtr->sizeHintsFlags |= PSize;
  1745.         } else {
  1746.         Tcl_AppendResult(interp, "bad argument \"", argv[3],
  1747.             "\": must be program or user", (char *) NULL);
  1748.         return TCL_ERROR;
  1749.         }
  1750.     }
  1751.     wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
  1752.     goto updateGeom;
  1753.     } else if ((c == 's') && (strncmp(argv[1], "state", length) == 0)
  1754.         && (length >= 2)) {
  1755.     if (argc != 3) {
  1756.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1757.             argv[0], " state window\"", (char *) NULL);
  1758.         return TCL_ERROR;
  1759.     }
  1760.     if (wmPtr->iconFor != NULL) {
  1761.         interp->result = "icon";
  1762.     } else if (wmPtr->withdrawn) {
  1763.         interp->result = "withdrawn";
  1764.     } else if (Tk_IsMapped((Tk_Window) winPtr)
  1765.         || ((wmPtr->flags & WM_NEVER_MAPPED)
  1766.         && (wmPtr->hints.initial_state == NormalState))) {
  1767. #ifdef STk_CODE
  1768.         interp->result = "\"normal\"";
  1769. #else
  1770.         interp->result = "normal";
  1771. #endif
  1772.     } else {
  1773. #ifdef STk_CODE
  1774.         interp->result = "\"iconic\"";
  1775. #else
  1776.         interp->result = "iconic";
  1777. #endif
  1778.     }
  1779.     } else if ((c == 't') && (strncmp(argv[1], "title", length) == 0)
  1780.         && (length >= 2)) {
  1781.     if (argc > 4) {
  1782.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1783.             argv[0], " title window ?newTitle?\"", (char *) NULL);
  1784.         return TCL_ERROR;
  1785.     }
  1786.     if (argc == 3) {
  1787. #ifdef STk_CODE
  1788.         STk_stringify_result(interp, (wmPtr->titleUid != NULL) ? wmPtr->titleUid
  1789.                          : winPtr->nameUid);
  1790. #else
  1791.         interp->result = (wmPtr->titleUid != NULL) ? wmPtr->titleUid
  1792.             : winPtr->nameUid;
  1793. #endif
  1794.         return TCL_OK;
  1795.     } else {
  1796.         wmPtr->titleUid = Tk_GetUid(argv[3]);
  1797.         if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  1798.         XTextProperty textProp;
  1799.  
  1800.         if (XStringListToTextProperty(&wmPtr->titleUid, 1,
  1801.             &textProp)  != 0) {
  1802.             XSetWMName(winPtr->display, winPtr->window, &textProp);
  1803.             XFree((char *) textProp.value);
  1804.         }
  1805.         }
  1806.     }
  1807.     } else if ((c == 't') && (strncmp(argv[1], "transient", length) == 0)
  1808.         && (length >= 3)) {
  1809.     Tk_Window master;
  1810.  
  1811.     if ((argc != 3) && (argc != 4)) {
  1812.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1813.             argv[0], " transient window ?master?\"", (char *) NULL);
  1814.         return TCL_ERROR;
  1815.     }
  1816.     if (argc == 3) {
  1817.         if (wmPtr->master != None) {
  1818.         interp->result = wmPtr->masterWindowName;
  1819.         }
  1820. #ifdef STk_CODE
  1821.         else {
  1822.             interp->result = "#f";
  1823.         }
  1824. #endif
  1825.         return TCL_OK;
  1826.     }
  1827. #ifdef STk_CODE
  1828.     if (argv[3][0] == '\0' || strcmp(argv[3], "#f") == 0) {
  1829. #else
  1830.     if (argv[3][0] == '\0') {
  1831. #endif
  1832.         wmPtr->master = None;
  1833.         wmPtr->masterWindowName = NULL;
  1834.     } else {
  1835.         master = Tk_NameToWindow(interp, argv[3], tkwin);
  1836.         if (master == NULL) {
  1837.         return TCL_ERROR;
  1838.         }
  1839.         Tk_MakeWindowExist(master);
  1840.         wmPtr->master = Tk_WindowId(master);
  1841.         wmPtr->masterWindowName = Tk_PathName(master);
  1842.     }
  1843.     if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  1844.         XSetTransientForHint(winPtr->display, winPtr->window,
  1845.             wmPtr->master);
  1846.     }
  1847.     } else if ((c == 'w') && (strncmp(argv[1], "withdraw", length) == 0)) {
  1848.     if (argc != 3) {
  1849.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1850.             argv[0], " withdraw window\"", (char *) NULL);
  1851.         return TCL_ERROR;
  1852.     }
  1853.     if (wmPtr->iconFor != NULL) {
  1854.         Tcl_AppendResult(interp, "can't withdraw ", argv[2],
  1855.             ": it is an icon for ", Tk_PathName(wmPtr->iconFor),
  1856.             (char *) NULL);
  1857.         return TCL_ERROR;
  1858.     }
  1859.     wmPtr->hints.initial_state = WithdrawnState;
  1860.     wmPtr->withdrawn = 1;
  1861.     if (wmPtr->flags & WM_NEVER_MAPPED) {
  1862.         return TCL_OK;
  1863.     }
  1864.     if (XWithdrawWindow(winPtr->display, winPtr->window,
  1865.         winPtr->screenNum) == 0) {
  1866.         interp->result =
  1867.             "couldn't send withdraw message to window manager";
  1868.         return TCL_ERROR;
  1869.     }
  1870.     WaitForMapNotify(winPtr, 0);
  1871.     } else {
  1872.     Tcl_AppendResult(interp, "unknown or ambiguous option \"", argv[1],
  1873.         "\": must be aspect, client, command, deiconify, ",
  1874.         "focusmodel, frame, geometry, grid, group, iconbitmap, ",
  1875.         "iconify, iconmask, iconname, iconposition, ",
  1876.         "iconwindow, maxsize, minsize, overrideredirect, ",
  1877.         "positionfrom, protocol, resizable, sizefrom, state, title, ",
  1878.         "transient, or withdraw",
  1879.         (char *) NULL);
  1880.     return TCL_ERROR;
  1881.     }
  1882.     return TCL_OK;
  1883.  
  1884.     updateGeom:
  1885.     if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
  1886.     Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) winPtr);
  1887.     wmPtr->flags |= WM_UPDATE_PENDING;
  1888.     }
  1889.     return TCL_OK;
  1890. }
  1891.  
  1892. /*
  1893.  *----------------------------------------------------------------------
  1894.  *
  1895.  * Tk_SetGrid --
  1896.  *
  1897.  *    This procedure is invoked by a widget when it wishes to set a grid
  1898.  *    coordinate system that controls the size of a top-level window.
  1899.  *    It provides a C interface equivalent to the "wm grid" command and
  1900.  *    is usually asscoiated with the -setgrid option.
  1901.  *
  1902.  * Results:
  1903.  *    None.
  1904.  *
  1905.  * Side effects:
  1906.  *    Grid-related information will be passed to the window manager, so
  1907.  *    that the top-level window associated with tkwin will resize on
  1908.  *    even grid units.  If some other window already controls gridding
  1909.  *    for the top-level window then this procedure call has no effect.
  1910.  *
  1911.  *----------------------------------------------------------------------
  1912.  */
  1913.  
  1914. void
  1915. Tk_SetGrid(tkwin, reqWidth, reqHeight, widthInc, heightInc)
  1916.     Tk_Window tkwin;        /* Token for window.  New window mgr info
  1917.                  * will be posted for the top-level window
  1918.                  * associated with this window. */
  1919.     int reqWidth;        /* Width (in grid units) corresponding to
  1920.                  * the requested geometry for tkwin. */
  1921.     int reqHeight;        /* Height (in grid units) corresponding to
  1922.                  * the requested geometry for tkwin. */
  1923.     int widthInc, heightInc;    /* Pixel increments corresponding to a
  1924.                  * change of one grid unit. */
  1925. {
  1926.     TkWindow *winPtr = (TkWindow *) tkwin;
  1927.     register WmInfo *wmPtr;
  1928.  
  1929.     /*
  1930.      * Find the top-level window for tkwin, plus the window manager
  1931.      * information.
  1932.      */
  1933.  
  1934.     while (!(winPtr->flags & TK_TOP_LEVEL)) {
  1935.     winPtr = winPtr->parentPtr;
  1936.     if (winPtr == NULL) {
  1937.         /*
  1938.          * The window is being deleted... just skip this operation.
  1939.          */
  1940.  
  1941.         return;
  1942.     }
  1943.     }
  1944.     wmPtr = winPtr->wmInfoPtr;
  1945.  
  1946.     if ((wmPtr->gridWin != NULL) && (wmPtr->gridWin != tkwin)) {
  1947.     return;
  1948.     }
  1949.  
  1950.     if ((wmPtr->reqGridWidth == reqWidth)
  1951.         && (wmPtr->reqGridHeight == reqHeight)
  1952.         && (wmPtr->widthInc == widthInc)
  1953.         && (wmPtr->heightInc == heightInc)
  1954.         && ((wmPtr->sizeHintsFlags & (PBaseSize|PResizeInc))
  1955.             == PBaseSize|PResizeInc)) {
  1956.     return;
  1957.     }
  1958.  
  1959.     /*
  1960.      * If gridding was previously off, then forget about any window
  1961.      * size requests made by the user or via "wm geometry":  these are
  1962.      * in pixel units and there's no easy way to translate them to
  1963.      * grid units since the new requested size of the top-level window in
  1964.      * pixels may not yet have been registered yet (it may filter up
  1965.      * the hierarchy in DoWhenIdle handlers).  However, if the window
  1966.      * has never been mapped yet then just leave the window size alone:
  1967.      * assume that it is intended to be in grid units but just happened
  1968.      * to have been specified before this procedure was called.
  1969.      */
  1970.  
  1971.     if ((wmPtr->gridWin == NULL) && !(wmPtr->flags & WM_NEVER_MAPPED)) {
  1972.     wmPtr->width = -1;
  1973.     wmPtr->height = -1;
  1974.     }
  1975.  
  1976.     /* 
  1977.      * Set the new gridding information, and start the process of passing
  1978.      * all of this information to the window manager.
  1979.      */
  1980.  
  1981.     wmPtr->gridWin = tkwin;
  1982.     wmPtr->reqGridWidth = reqWidth;
  1983.     wmPtr->reqGridHeight = reqHeight;
  1984.     wmPtr->widthInc = widthInc;
  1985.     wmPtr->heightInc = heightInc;
  1986.     wmPtr->sizeHintsFlags |= PBaseSize|PResizeInc;
  1987.     wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
  1988.     if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
  1989.     Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) winPtr);
  1990.     wmPtr->flags |= WM_UPDATE_PENDING;
  1991.     }
  1992. }
  1993.  
  1994. /*
  1995.  *----------------------------------------------------------------------
  1996.  *
  1997.  * Tk_UnsetGrid --
  1998.  *
  1999.  *    This procedure cancels the effect of a previous call
  2000.  *    to Tk_SetGrid.
  2001.  *
  2002.  * Results:
  2003.  *    None.
  2004.  *
  2005.  * Side effects:
  2006.  *    If tkwin currently controls gridding for its top-level window,
  2007.  *    gridding is cancelled for that top-level window;  if some other
  2008.  *    window controls gridding then this procedure has no effect.
  2009.  *
  2010.  *----------------------------------------------------------------------
  2011.  */
  2012.  
  2013. void
  2014. Tk_UnsetGrid(tkwin)
  2015.     Tk_Window tkwin;        /* Token for window that is currently
  2016.                  * controlling gridding. */
  2017. {
  2018.     TkWindow *winPtr = (TkWindow *) tkwin;
  2019.     register WmInfo *wmPtr;
  2020.  
  2021.     /*
  2022.      * Find the top-level window for tkwin, plus the window manager
  2023.      * information.
  2024.      */
  2025.  
  2026.     while (!(winPtr->flags & TK_TOP_LEVEL)) {
  2027.     winPtr = winPtr->parentPtr;
  2028.     if (winPtr == NULL) {
  2029.         /*
  2030.          * The window is being deleted... just skip this operation.
  2031.          */
  2032.  
  2033.         return;
  2034.     }
  2035.     }
  2036.     wmPtr = winPtr->wmInfoPtr;
  2037.     if (tkwin != wmPtr->gridWin) {
  2038.     return;
  2039.     }
  2040.  
  2041.     wmPtr->gridWin = NULL;
  2042.     wmPtr->sizeHintsFlags &= ~(PBaseSize|PResizeInc);
  2043.     if (wmPtr->width != -1) {
  2044.     wmPtr->width = winPtr->reqWidth + (wmPtr->width
  2045.         - wmPtr->reqGridWidth)*wmPtr->widthInc;
  2046.     wmPtr->height = winPtr->reqHeight + (wmPtr->height
  2047.         - wmPtr->reqGridHeight)*wmPtr->heightInc;
  2048.     }
  2049.     wmPtr->widthInc = 1;
  2050.     wmPtr->heightInc = 1;
  2051.  
  2052.     wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
  2053.     if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
  2054.     Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) winPtr);
  2055.     wmPtr->flags |= WM_UPDATE_PENDING;
  2056.     }
  2057. }
  2058.  
  2059. /*
  2060.  *----------------------------------------------------------------------
  2061.  *
  2062.  * ConfigureEvent --
  2063.  *
  2064.  *    This procedure is called to handle ConfigureNotify events on
  2065.  *    top-level windows.
  2066.  *
  2067.  * Results:
  2068.  *    None.
  2069.  *
  2070.  * Side effects:
  2071.  *    Information gets updated in the WmInfo structure for the window.
  2072.  *
  2073.  *----------------------------------------------------------------------
  2074.  */
  2075.  
  2076. static void
  2077. ConfigureEvent(winPtr, configEventPtr)
  2078.     TkWindow *winPtr;            /* Top-level window. */
  2079.     XConfigureEvent *configEventPtr;    /* Event that just occurred for
  2080.                      * winPtr. */
  2081. {
  2082.     register WmInfo *wmPtr = winPtr->wmInfoPtr;
  2083.  
  2084.     /* 
  2085.      * Update size information from the event.  There are a couple of
  2086.      * tricky points here:
  2087.      *
  2088.      * 1. If the user changed the size externally then set wmPtr->width
  2089.      *    and wmPtr->height just as if a "wm geometry" command had been
  2090.      *    invoked with the same information.
  2091.      * 2. However, if the size is changing in response to a request
  2092.      *    coming from us (WM_SYNC_PENDING is set), then don't set wmPtr->width
  2093.      *    or wmPtr->height if they were previously -1 (otherwise the
  2094.      *    window will stop tracking geometry manager requests).
  2095.      */
  2096.  
  2097.     if (((winPtr->changes.width != configEventPtr->width)
  2098.         || (winPtr->changes.height != configEventPtr->height))
  2099.         && !(wmPtr->flags & WM_SYNC_PENDING)){
  2100.     if (wmTracing) {
  2101.         printf("TopLevelEventProc: user changed %s size to %dx%d\n",
  2102.             winPtr->pathName, configEventPtr->width,
  2103.             configEventPtr->height);
  2104.     }
  2105.     if ((wmPtr->width == -1)
  2106.         && (configEventPtr->width == winPtr->reqWidth)) {
  2107.         /*
  2108.          * Don't set external width, since the user didn't change it
  2109.          * from what the widgets asked for.
  2110.          */
  2111.     } else {
  2112.         if (wmPtr->gridWin != NULL) {
  2113.         wmPtr->width = wmPtr->reqGridWidth
  2114.             + (configEventPtr->width
  2115.             - winPtr->reqWidth)/wmPtr->widthInc;
  2116.         if (wmPtr->width < 0) {
  2117.             wmPtr->width = 0;
  2118.         }
  2119.         } else {
  2120.         wmPtr->width = configEventPtr->width;
  2121.         }
  2122.     }
  2123.     if ((wmPtr->height == -1)
  2124.         && (configEventPtr->height == winPtr->reqHeight)) {
  2125.         /*
  2126.          * Don't set external height, since the user didn't change it
  2127.          * from what the widgets asked for.
  2128.          */
  2129.     } else {
  2130.         if (wmPtr->gridWin != NULL) {
  2131.         wmPtr->height = wmPtr->reqGridHeight
  2132.             + (configEventPtr->height
  2133.             - winPtr->reqHeight)/wmPtr->heightInc;
  2134.         if (wmPtr->height < 0) {
  2135.             wmPtr->height = 0;
  2136.         }
  2137.         } else {
  2138.         wmPtr->height = configEventPtr->height;
  2139.         }
  2140.     }
  2141.     wmPtr->configWidth = configEventPtr->width;
  2142.     wmPtr->configHeight = configEventPtr->height;
  2143.     }
  2144.  
  2145.     if (wmTracing) {
  2146.     printf("ConfigureEvent: %s x = %d y = %d, width = %d, height = %d",
  2147.         winPtr->pathName, configEventPtr->x, configEventPtr->y,
  2148.         configEventPtr->width, configEventPtr->height);
  2149.     printf(" send_event = %d, serial = %ld\n", configEventPtr->send_event,
  2150.         configEventPtr->serial);
  2151.     }
  2152.     winPtr->changes.width = configEventPtr->width;
  2153.     winPtr->changes.height = configEventPtr->height;
  2154.     winPtr->changes.border_width = configEventPtr->border_width;
  2155.     winPtr->changes.sibling = configEventPtr->above;
  2156.     winPtr->changes.stack_mode = Above;
  2157.  
  2158.     /*
  2159.      * Reparenting window managers make life difficult.  If the
  2160.      * window manager reparents a top-level window then the x and y
  2161.      * information that comes in events for the window is wrong:
  2162.      * it gives the location of the window inside its decorative
  2163.      * parent, rather than the location of the window in root
  2164.      * coordinates, which is what we want.  Window managers
  2165.      * are supposed to send synthetic events with the correct
  2166.      * information, but ICCCM doesn't require them to do this
  2167.      * under all conditions, and the information provided doesn't
  2168.      * include everything we need here.  So, the code below
  2169.      * maintains a bunch of information about the parent window.
  2170.      * If the window hasn't been reparented, we pretend that
  2171.      * there is a parent shrink-wrapped around the window.
  2172.      */
  2173.  
  2174.     if ((wmPtr->reparent == None) || !ComputeReparentGeometry(winPtr)) {
  2175.     wmPtr->parentWidth = configEventPtr->width
  2176.         + 2*configEventPtr->border_width;
  2177.     wmPtr->parentHeight = configEventPtr->height
  2178.         + 2*configEventPtr->border_width;
  2179.     winPtr->changes.x = wmPtr->x = configEventPtr->x;
  2180.     winPtr->changes.y = wmPtr->y = configEventPtr->y;
  2181.     if (wmPtr->flags & WM_NEGATIVE_X) {
  2182.         wmPtr->x = wmPtr->vRootWidth - (wmPtr->x + wmPtr->parentWidth);
  2183.     }
  2184.     if (wmPtr->flags & WM_NEGATIVE_Y) {
  2185.         wmPtr->y = wmPtr->vRootHeight - (wmPtr->y + wmPtr->parentHeight);
  2186.     }
  2187.     }
  2188. }
  2189.  
  2190. /*
  2191.  *----------------------------------------------------------------------
  2192.  *
  2193.  * ReparentEvent --
  2194.  *
  2195.  *    This procedure is called to handle ReparentNotify events on
  2196.  *    top-level windows.
  2197.  *
  2198.  * Results:
  2199.  *    None.
  2200.  *
  2201.  * Side effects:
  2202.  *    Information gets updated in the WmInfo structure for the window.
  2203.  *
  2204.  *----------------------------------------------------------------------
  2205.  */
  2206.  
  2207. static void
  2208. ReparentEvent(winPtr, reparentEventPtr)
  2209.     TkWindow *winPtr;            /* Top-level window. */
  2210.     XReparentEvent *reparentEventPtr;    /* Event that just occurred for
  2211.                      * winPtr. */
  2212. {
  2213.     register WmInfo *wmPtr = winPtr->wmInfoPtr;
  2214.     Window vRoot, ancestor, *children, dummy2, *virtualRootPtr;
  2215.     Atom actualType;
  2216.     int actualFormat;
  2217.     unsigned long numItems, bytesAfter;
  2218.     unsigned int dummy;
  2219.     Tk_ErrorHandler handler;
  2220.  
  2221.     /*
  2222.      * Identify the root window for winPtr.  This is tricky because of
  2223.      * virtual root window managers like tvtwm.  If the window has a
  2224.      * property named __SWM_ROOT or __WM_ROOT then this property gives
  2225.      * the id for a virtual root window that should be used instead of
  2226.      * the root window of the screen.
  2227.      */
  2228.  
  2229.     vRoot = RootWindow(winPtr->display, winPtr->screenNum);
  2230.     wmPtr->vRoot = None;
  2231.     handler = Tk_CreateErrorHandler(winPtr->display, -1, -1, -1,
  2232.         (Tk_ErrorProc *) NULL, (ClientData) NULL);
  2233.     if (((XGetWindowProperty(winPtr->display, winPtr->window,
  2234.         Tk_InternAtom((Tk_Window) winPtr, "__WM_ROOT"), 0, (long) 1,
  2235.         False, XA_WINDOW, &actualType, &actualFormat, &numItems,
  2236.         &bytesAfter, (unsigned char **) &virtualRootPtr) == Success)
  2237.         && (actualType == XA_WINDOW))
  2238.         || ((XGetWindowProperty(winPtr->display, winPtr->window,
  2239.         Tk_InternAtom((Tk_Window) winPtr, "__SWM_ROOT"), 0, (long) 1,
  2240.         False, XA_WINDOW, &actualType, &actualFormat, &numItems,
  2241.         &bytesAfter, (unsigned char **) &virtualRootPtr) == Success)
  2242.         && (actualType == XA_WINDOW))) {
  2243.     if ((actualFormat == 32) && (numItems == 1)) {
  2244.         vRoot = wmPtr->vRoot = *virtualRootPtr;
  2245.     } else if (wmTracing) {
  2246.         printf("%s format %d numItems %ld\n",
  2247.             "ReparentEvent got bogus VROOT property:", actualFormat,
  2248.             numItems);
  2249.     }
  2250.     XFree((char *) virtualRootPtr);
  2251.     }
  2252.     Tk_DeleteErrorHandler(handler);
  2253.  
  2254.     if (wmTracing) {
  2255.     printf("ReparentEvent: %s reparented to 0x%x, vRoot = 0x%x\n",
  2256.         winPtr->pathName, (unsigned int) reparentEventPtr->parent,
  2257.         (unsigned int) vRoot);
  2258.     }
  2259.  
  2260.     /*
  2261.      * Fetch correct geometry information for the new virtual root.
  2262.      */
  2263.  
  2264.     UpdateVRootGeometry(wmPtr);
  2265.  
  2266.     /*
  2267.      * If the window's new parent is the root window, then mark it as
  2268.      * no longer reparented.
  2269.      */
  2270.  
  2271.     if (reparentEventPtr->parent == vRoot) {
  2272.     noReparent:
  2273.     wmPtr->reparent = None;
  2274.     wmPtr->parentWidth = winPtr->changes.width
  2275.         + 2*winPtr->changes.border_width;
  2276.     wmPtr->parentHeight = winPtr->changes.height
  2277.         + 2*winPtr->changes.border_width;
  2278.     wmPtr->xInParent = wmPtr->yInParent = 0;
  2279.     winPtr->changes.x = reparentEventPtr->x;
  2280.     winPtr->changes.y = reparentEventPtr->y;
  2281.     return;
  2282.     }
  2283.  
  2284.     /*
  2285.      * Search up the window hierarchy to find the ancestor of this
  2286.      * window that is just below the (virtual) root.  This is tricky
  2287.      * because it's possible that things have changed since the event
  2288.      * was generated so that the ancestry indicated by the event no
  2289.      * longer exists.  If this happens then an error will occur and
  2290.      * we just discard the event (there will be a more up-to-date
  2291.      * ReparentNotify event coming later).
  2292.      */
  2293.  
  2294.     handler = Tk_CreateErrorHandler(winPtr->display, -1, -1, -1,
  2295.         (Tk_ErrorProc *) NULL, (ClientData) NULL);
  2296.     wmPtr->reparent = reparentEventPtr->parent;
  2297.     while (1) {
  2298.     if (XQueryTree(winPtr->display, wmPtr->reparent, &dummy2, &ancestor,
  2299.         &children, &dummy) == 0) {
  2300.         Tk_DeleteErrorHandler(handler);
  2301.         goto noReparent;
  2302.     }
  2303.     XFree((char *) children);
  2304.     if ((ancestor == vRoot) ||
  2305.         (ancestor == RootWindow(winPtr->display, winPtr->screenNum))) {
  2306.         break;
  2307.     }
  2308.     wmPtr->reparent = ancestor;
  2309.     }
  2310.     Tk_DeleteErrorHandler(handler);
  2311.  
  2312.     if (!ComputeReparentGeometry(winPtr)) {
  2313.     goto noReparent;
  2314.     }
  2315. }
  2316.  
  2317. /*
  2318.  *----------------------------------------------------------------------
  2319.  *
  2320.  * ComputeReparentGeometry --
  2321.  *
  2322.  *    This procedure is invoked to recompute geometry information
  2323.  *    related to a reparented top-level window, such as the position
  2324.  *    and total size of the parent and the position within it of
  2325.  *    the top-level window.
  2326.  *
  2327.  * Results:
  2328.  *    The return value is 1 if everything completed successfully
  2329.  *    and 0 if an error occurred while querying information about
  2330.  *    winPtr's parents.  In this case winPtr is marked as no longer
  2331.  *    being reparented.
  2332.  *
  2333.  * Side effects:
  2334.  *    Geometry information in winPtr and winPtr->wmPtr gets updated.
  2335.  *
  2336.  *----------------------------------------------------------------------
  2337.  */
  2338.  
  2339. static int
  2340. ComputeReparentGeometry(winPtr)
  2341.     TkWindow *winPtr;        /* Top-level window whose reparent info
  2342.                  * is to be recomputed. */
  2343. {
  2344.     register WmInfo *wmPtr = winPtr->wmInfoPtr;
  2345.     int width, height, bd;
  2346.     unsigned int dummy;
  2347.     int xOffset, yOffset, x, y;
  2348.     Window dummy2;
  2349.     Status status;
  2350.     Tk_ErrorHandler handler;
  2351.  
  2352.     handler = Tk_CreateErrorHandler(winPtr->display, -1, -1, -1,
  2353.         (Tk_ErrorProc *) NULL, (ClientData) NULL);
  2354.     (void) XTranslateCoordinates(winPtr->display, winPtr->window,
  2355.         wmPtr->reparent, 0, 0, &xOffset, &yOffset, &dummy2);
  2356.     status = XGetGeometry(winPtr->display, wmPtr->reparent,
  2357.         &dummy2, &x, &y, (unsigned int *) &width,
  2358.         (unsigned int *) &height, (unsigned int *) &bd, &dummy);
  2359.     Tk_DeleteErrorHandler(handler);
  2360.     if (status == 0) {
  2361.     /*
  2362.      * It appears that the reparented parent went away and
  2363.      * no-one told us.  Reset the window to indicate that
  2364.      * it's not reparented.
  2365.      */
  2366.     wmPtr->reparent = None;
  2367.     wmPtr->xInParent = wmPtr->yInParent = 0;
  2368.     return 0;
  2369.     }
  2370.     wmPtr->xInParent = xOffset + bd - winPtr->changes.border_width;
  2371.     wmPtr->yInParent = yOffset + bd - winPtr->changes.border_width;
  2372.     wmPtr->parentWidth = width + 2*bd;
  2373.     wmPtr->parentHeight = height + 2*bd;
  2374.  
  2375.     /*
  2376.      * Some tricky issues in updating wmPtr->x and wmPtr->y:
  2377.      *
  2378.      * 1. Don't update them if the event occurred because of something
  2379.      * we did (i.e. WM_SYNC_PENDING and WM_MOVE_PENDING are both set).
  2380.      * This is because window managers treat coords differently than Tk,
  2381.      * and no two window managers are alike. If the window manager moved
  2382.      * the window because we told it to, remember the coordinates we told
  2383.      * it, not the ones it actually moved it to.  This allows us to move
  2384.      * the window back to the same coordinates later and get the same
  2385.      * result. Without this check, windows can "walk" across the screen
  2386.      * under some conditions.
  2387.      *
  2388.      * 2. Don't update wmPtr->x and wmPtr->y unless winPtr->changes.x
  2389.      * or winPtr->changes.y has changed (otherwise a size change can
  2390.      * spoof us into thinking that the position changed too and defeat
  2391.      * the intent of (1) above.
  2392.      *
  2393.      * 3. Ignore size changes coming from the window system if we're
  2394.      * about to change the size ourselves but haven't seen the event for
  2395.      * it yet:  our size change is supposed to take priority.
  2396.      */
  2397.  
  2398.     if (!(wmPtr->flags & WM_MOVE_PENDING)
  2399.         && ((winPtr->changes.x != (x + wmPtr->xInParent))
  2400.         || (winPtr->changes.y != (y + wmPtr->yInParent)))) {
  2401.     wmPtr->x = x;
  2402.     if (wmPtr->flags & WM_NEGATIVE_X) {
  2403.         wmPtr->x = wmPtr->vRootWidth - (wmPtr->x + wmPtr->parentWidth);
  2404.     }
  2405.     wmPtr->y = y;
  2406.     if (wmPtr->flags & WM_NEGATIVE_Y) {
  2407.         wmPtr->y = wmPtr->vRootHeight - (wmPtr->y + wmPtr->parentHeight);
  2408.     }
  2409.     }
  2410.  
  2411.     winPtr->changes.x = x + wmPtr->xInParent;
  2412.     winPtr->changes.y = y + wmPtr->yInParent;
  2413.     if (wmTracing) {
  2414.     printf("winPtr coords %d,%d, wmPtr coords %d,%d, offsets %d %d\n",
  2415.         winPtr->changes.x, winPtr->changes.y, wmPtr->x, wmPtr->y,
  2416.         wmPtr->xInParent, wmPtr->yInParent);
  2417.     }
  2418.     return 1;
  2419. }
  2420.  
  2421. /*
  2422.  *----------------------------------------------------------------------
  2423.  *
  2424.  * TopLevelEventProc --
  2425.  *
  2426.  *    This procedure is invoked when a top-level (or other externally-
  2427.  *    managed window) is restructured in any way.
  2428.  *
  2429.  * Results:
  2430.  *    None.
  2431.  *
  2432.  * Side effects:
  2433.  *    Tk's internal data structures for the window get modified to
  2434.  *    reflect the structural change.
  2435.  *
  2436.  *----------------------------------------------------------------------
  2437.  */
  2438.  
  2439. static void
  2440. TopLevelEventProc(clientData, eventPtr)
  2441.     ClientData clientData;        /* Window for which event occurred. */
  2442.     XEvent *eventPtr;            /* Event that just happened. */
  2443. {
  2444.     register TkWindow *winPtr = (TkWindow *) clientData;
  2445.  
  2446.     winPtr->wmInfoPtr->flags |= WM_VROOT_OFFSET_STALE;
  2447.     if (eventPtr->type == DestroyNotify) {
  2448.     Tk_ErrorHandler handler;
  2449.  
  2450.     if (!(winPtr->flags & TK_ALREADY_DEAD)) {
  2451.         /*
  2452.          * A top-level window was deleted externally (e.g., by the window
  2453.          * manager).  This is probably not a good thing, but cleanup as
  2454.          * best we can.  The error handler is needed because
  2455.          * Tk_DestroyWindow will try to destroy the window, but of course
  2456.          * it's already gone.
  2457.          */
  2458.     
  2459.         handler = Tk_CreateErrorHandler(winPtr->display, -1, -1, -1,
  2460.             (Tk_ErrorProc *) NULL, (ClientData) NULL);
  2461.         Tk_DestroyWindow((Tk_Window) winPtr);
  2462.         Tk_DeleteErrorHandler(handler);
  2463.     }
  2464.     if (wmTracing) {
  2465.         printf("TopLevelEventProc: %s deleted\n", winPtr->pathName);
  2466.     }
  2467.     } else if (eventPtr->type == ConfigureNotify) {
  2468.     /*
  2469.      * Ignore the event if the window has never been mapped yet.
  2470.      * Such an event occurs only in weird cases like changing the
  2471.      * internal border width of a top-level window, which results
  2472.      * in a synthetic Configure event.  These events are not relevant
  2473.      * to us, and if we process them confusion may result (e.g. we
  2474.      * may conclude erroneously that the user repositioned or resized
  2475.      * the window).
  2476.      */
  2477.  
  2478.     if (!(winPtr->wmInfoPtr->flags & WM_NEVER_MAPPED)) {
  2479.         ConfigureEvent(winPtr, &eventPtr->xconfigure);
  2480.     }
  2481.     } else if (eventPtr->type == MapNotify) {
  2482.     winPtr->flags |= TK_MAPPED;
  2483.     if (wmTracing) {
  2484.         printf("TopLevelEventProc: %s mapped\n", winPtr->pathName);
  2485.     }
  2486.     } else if (eventPtr->type == UnmapNotify) {
  2487.     winPtr->flags &= ~TK_MAPPED;
  2488.     if (wmTracing) {
  2489.         printf("TopLevelEventProc: %s unmapped\n", winPtr->pathName);
  2490.     }
  2491.     } else if (eventPtr->type == ReparentNotify) {
  2492.     ReparentEvent(winPtr, &eventPtr->xreparent);
  2493.     }
  2494. }
  2495.  
  2496. /*
  2497.  *----------------------------------------------------------------------
  2498.  *
  2499.  * TopLevelReqProc --
  2500.  *
  2501.  *    This procedure is invoked by the geometry manager whenever
  2502.  *    the requested size for a top-level window is changed.
  2503.  *
  2504.  * Results:
  2505.  *    None.
  2506.  *
  2507.  * Side effects:
  2508.  *    Arrange for the window to be resized to satisfy the request
  2509.  *    (this happens as a when-idle action).
  2510.  *
  2511.  *----------------------------------------------------------------------
  2512.  */
  2513.  
  2514.     /* ARGSUSED */
  2515. static void
  2516. TopLevelReqProc(dummy, tkwin)
  2517.     ClientData dummy;            /* Not used. */
  2518.     Tk_Window tkwin;            /* Information about window. */
  2519. {
  2520.     TkWindow *winPtr = (TkWindow *) tkwin;
  2521.     WmInfo *wmPtr;
  2522.  
  2523.     wmPtr = winPtr->wmInfoPtr;
  2524.     wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
  2525.     if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
  2526.     Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) winPtr);
  2527.     wmPtr->flags |= WM_UPDATE_PENDING;
  2528.     }
  2529.  
  2530.     /*
  2531.      * If the window isn't being positioned by its upper left corner
  2532.      * then we have to move it as well.
  2533.      */
  2534.  
  2535.     if (wmPtr->flags & (WM_NEGATIVE_X | WM_NEGATIVE_Y)) {
  2536.     wmPtr->flags |= WM_MOVE_PENDING;
  2537.     }
  2538. }
  2539.  
  2540. /*
  2541.  *----------------------------------------------------------------------
  2542.  *
  2543.  * UpdateGeometryInfo --
  2544.  *
  2545.  *    This procedure is invoked when a top-level window is first
  2546.  *    mapped, and also as a when-idle procedure, to bring the
  2547.  *    geometry and/or position of a top-level window back into
  2548.  *    line with what has been requested by the user and/or widgets.
  2549.  *    This procedure doesn't return until the window manager has
  2550.  *    responded to the geometry change.
  2551.  *
  2552.  * Results:
  2553.  *    None.
  2554.  *
  2555.  * Side effects:
  2556.  *    The window's size and location may change, unless the WM prevents
  2557.  *    that from happening.
  2558.  *
  2559.  *----------------------------------------------------------------------
  2560.  */
  2561.  
  2562. static void
  2563. UpdateGeometryInfo(clientData)
  2564.     ClientData clientData;        /* Pointer to the window's record. */
  2565. {
  2566.     register TkWindow *winPtr = (TkWindow *) clientData;
  2567.     register WmInfo *wmPtr = winPtr->wmInfoPtr;
  2568.     int x, y, width, height;
  2569.     unsigned long serial;
  2570.  
  2571.     wmPtr->flags &= ~WM_UPDATE_PENDING;
  2572.  
  2573.     /*
  2574.      * Compute the new size for the top-level window.  See the
  2575.      * user documentation for details on this, but the size
  2576.      * requested depends on (a) the size requested internally
  2577.      * by the window's widgets, (b) the size requested by the
  2578.      * user in a "wm geometry" command or via wm-based interactive
  2579.      * resizing (if any), and (c) whether or not the window is
  2580.      * gridded.  Don't permit sizes <= 0 because this upsets
  2581.      * the X server.
  2582.      */
  2583.  
  2584.     if (wmPtr->width == -1) {
  2585.     width = winPtr->reqWidth;
  2586.     } else if (wmPtr->gridWin != NULL) {
  2587.     width = winPtr->reqWidth
  2588.         + (wmPtr->width - wmPtr->reqGridWidth)*wmPtr->widthInc;
  2589.     } else {
  2590.     width = wmPtr->width;
  2591.     }
  2592.     if (width <= 0) {
  2593.     width = 1;
  2594.     }
  2595.     if (wmPtr->height == -1) {
  2596.     height = winPtr->reqHeight;
  2597.     } else if (wmPtr->gridWin != NULL) {
  2598.     height = winPtr->reqHeight
  2599.         + (wmPtr->height - wmPtr->reqGridHeight)*wmPtr->heightInc;
  2600.     } else {
  2601.     height = wmPtr->height;
  2602.     }
  2603.     if (height <= 0) {
  2604.     height = 1;
  2605.     }
  2606.  
  2607.     /*
  2608.      * Compute the new position for the upper-left pixel of the window's
  2609.      * decorative frame.  This is tricky, because we need to include the
  2610.      * border widths supplied by a reparented parent in this calculation,
  2611.      * but can't use the parent's current overall size since that may
  2612.      * change as a result of this code.
  2613.      */
  2614.  
  2615.     if (wmPtr->flags & WM_NEGATIVE_X) {
  2616.     x = wmPtr->vRootWidth - wmPtr->x
  2617.         - (width + (wmPtr->parentWidth - winPtr->changes.width));
  2618.     } else {
  2619.     x =  wmPtr->x;
  2620.     }
  2621.     if (wmPtr->flags & WM_NEGATIVE_Y) {
  2622.     y = wmPtr->vRootHeight - wmPtr->y
  2623.         - (height + (wmPtr->parentHeight - winPtr->changes.height));
  2624.     } else {
  2625.     y =  wmPtr->y;
  2626.     }
  2627.  
  2628.     /*
  2629.      * If the window's size is going to change and the window is
  2630.      * supposed to not be resizable by the user, then we have to
  2631.      * update the size hints.  There may also be a size-hint-update
  2632.      * request pending from somewhere else, too.
  2633.      */
  2634.  
  2635.     if (((width != winPtr->changes.width)
  2636.         || (height != winPtr->changes.height))
  2637.         && (wmPtr->gridWin == NULL)
  2638.         && ((wmPtr->sizeHintsFlags & (PMinSize|PMaxSize)) == 0)) {
  2639.     wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
  2640.     }
  2641.     if (wmPtr->flags & WM_UPDATE_SIZE_HINTS) {
  2642.     UpdateSizeHints(winPtr);
  2643.     }
  2644.  
  2645.     /*
  2646.      * Reconfigure the window if it isn't already configured correctly.
  2647.      * A few tricky points:
  2648.      *
  2649.      * 1. Sometimes the window manager will give us a different size
  2650.      *    than we asked for (e.g. mwm has a minimum size for windows), so
  2651.      *    base the size check on what we *asked for* last time, not what we
  2652.      *    got.
  2653.      * 2. Can't just reconfigure always, because we may not get a
  2654.      *    ConfigureNotify event back if nothing changed, so
  2655.      *    WaitForConfigureNotify will hang a long time.
  2656.      * 3. Don't move window unless a new position has been requested for
  2657.      *      it.  This is because of "features" in some window managers (e.g.
  2658.      *    twm, as of 4/24/91) where they don't interpret coordinates
  2659.      *    according to ICCCM.  Moving a window to its current location may
  2660.      *    cause it to shift position on the screen.
  2661.      */
  2662.  
  2663.     serial = NextRequest(winPtr->display);
  2664.     if (wmPtr->flags & WM_MOVE_PENDING) {
  2665.     wmPtr->configWidth = width;
  2666.     wmPtr->configHeight = height;
  2667.     if (wmTracing) {
  2668.         printf("UpdateGeometryInfo moving to %d %d, resizing to %d x %d,\n",
  2669.             x, y, width, height);
  2670.     }
  2671.     Tk_MoveResizeWindow((Tk_Window) winPtr, x, y, width, height);
  2672.     } else if ((width != wmPtr->configWidth)
  2673.         || (height != wmPtr->configHeight)) {
  2674.     wmPtr->configWidth = width;
  2675.     wmPtr->configHeight = height;
  2676.     if (wmTracing) {
  2677.         printf("UpdateGeometryInfo resizing to %d x %d\n", width, height);
  2678.     }
  2679.     Tk_ResizeWindow((Tk_Window) winPtr, width, height);
  2680.     } else {
  2681.     return;
  2682.     }
  2683.  
  2684.     /*
  2685.      * Wait for the configure operation to complete.  Don't need to do
  2686.      * this, however, if the window is about to be mapped:  it will be
  2687.      * taken care of elsewhere.
  2688.      */
  2689.  
  2690.     if (!(wmPtr->flags & WM_ABOUT_TO_MAP)) {
  2691.     WaitForConfigureNotify(winPtr, serial);
  2692.     }
  2693. }
  2694.  
  2695. /*
  2696.  *--------------------------------------------------------------
  2697.  *
  2698.  * UpdateSizeHints --
  2699.  *
  2700.  *    This procedure is called to update the window manager's
  2701.  *    size hints information from the information in a WmInfo
  2702.  *    structure.
  2703.  *
  2704.  * Results:
  2705.  *    None.
  2706.  *
  2707.  * Side effects:
  2708.  *    Properties get changed for winPtr.
  2709.  *
  2710.  *--------------------------------------------------------------
  2711.  */
  2712.  
  2713. static void
  2714. UpdateSizeHints(winPtr)
  2715.     TkWindow *winPtr;
  2716. {
  2717.     register WmInfo *wmPtr = winPtr->wmInfoPtr;
  2718.     XSizeHints *hintsPtr;
  2719.     int maxWidth, maxHeight;
  2720.  
  2721.     wmPtr->flags &= ~WM_UPDATE_SIZE_HINTS;
  2722.  
  2723.     hintsPtr = XAllocSizeHints();
  2724.     if (hintsPtr == NULL) {
  2725.     return;
  2726.     }
  2727.  
  2728.     /*
  2729.      * Compute the pixel-based sizes for the various fields in the
  2730.      * size hints structure, based on the grid-based sizes in
  2731.      * our structure.
  2732.      */
  2733.  
  2734.     GetMaxSize(wmPtr, &maxWidth, &maxHeight);
  2735.     if (wmPtr->gridWin != NULL) {
  2736.     hintsPtr->base_width = winPtr->reqWidth
  2737.         - (wmPtr->reqGridWidth * wmPtr->widthInc);
  2738.     if (hintsPtr->base_width < 0) {
  2739.         hintsPtr->base_width = 0;
  2740.     }
  2741.     hintsPtr->base_height = winPtr->reqHeight
  2742.         - (wmPtr->reqGridHeight * wmPtr->heightInc);
  2743.     if (hintsPtr->base_height < 0) {
  2744.         hintsPtr->base_height = 0;
  2745.     }
  2746.     hintsPtr->min_width = hintsPtr->base_width
  2747.         + (wmPtr->minWidth * wmPtr->widthInc);
  2748.     hintsPtr->min_height = hintsPtr->base_height
  2749.         + (wmPtr->minHeight * wmPtr->heightInc);
  2750.     hintsPtr->max_width = hintsPtr->base_width
  2751.         + (maxWidth * wmPtr->widthInc);
  2752.     hintsPtr->max_height = hintsPtr->base_height
  2753.         + (maxHeight * wmPtr->heightInc);
  2754.     } else {
  2755.     hintsPtr->min_width = wmPtr->minWidth;
  2756.     hintsPtr->min_height = wmPtr->minHeight;
  2757.     hintsPtr->max_width = maxWidth;
  2758.     hintsPtr->max_height = maxHeight;
  2759.     hintsPtr->base_width = 0;
  2760.     hintsPtr->base_height = 0;
  2761.     }
  2762.     hintsPtr->width_inc = wmPtr->widthInc;
  2763.     hintsPtr->height_inc = wmPtr->heightInc;
  2764.     hintsPtr->min_aspect.x = wmPtr->minAspect.x;
  2765.     hintsPtr->min_aspect.y = wmPtr->minAspect.y;
  2766.     hintsPtr->max_aspect.x = wmPtr->maxAspect.x;
  2767.     hintsPtr->max_aspect.y = wmPtr->maxAspect.y;
  2768.     hintsPtr->win_gravity = wmPtr->gravity;
  2769.     hintsPtr->flags = wmPtr->sizeHintsFlags | PMinSize | PMaxSize;
  2770.  
  2771.     /*
  2772.      * If the window isn't supposed to be resizable, then set the
  2773.      * minimum and maximum dimensions to be the same.
  2774.      */
  2775.  
  2776.     if (wmPtr->flags & WM_WIDTH_NOT_RESIZABLE) {
  2777.     if (wmPtr->width >= 0) {
  2778.         hintsPtr->min_width = wmPtr->width;
  2779.     } else {
  2780.         hintsPtr->min_width = winPtr->reqWidth;
  2781.     }
  2782.     hintsPtr->max_width = hintsPtr->min_width;
  2783.     }
  2784.     if (wmPtr->flags & WM_HEIGHT_NOT_RESIZABLE) {
  2785.     if (wmPtr->height >= 0) {
  2786.         hintsPtr->min_height = wmPtr->height;
  2787.     } else {
  2788.         hintsPtr->min_height = winPtr->reqHeight;
  2789.     }
  2790.     hintsPtr->max_height = hintsPtr->min_height;
  2791.     }
  2792.  
  2793.     XSetWMNormalHints(winPtr->display, winPtr->window, hintsPtr);
  2794.  
  2795.     XFree((char *) hintsPtr);
  2796. }
  2797.  
  2798. /*
  2799.  *----------------------------------------------------------------------
  2800.  *
  2801.  * WaitForConfigureNotify --
  2802.  *
  2803.  *    This procedure is invoked in order to synchronize with the
  2804.  *    window manager.  It waits for a ConfigureNotify event to
  2805.  *    arrive, signalling that the window manager has seen an attempt
  2806.  *    on our part to move or resize a top-level window.
  2807.  *
  2808.  * Results:
  2809.  *    None.
  2810.  *
  2811.  * Side effects:
  2812.  *    Delays the execution of the process until a ConfigureNotify event
  2813.  *    arrives with serial number at least as great as serial.  This
  2814.  *    is useful for two reasons:
  2815.  *
  2816.  *    1. It's important to distinguish ConfigureNotify events that are
  2817.  *       coming in response to a request we've made from those generated
  2818.  *       spontaneously by the user.  The reason for this is that if the
  2819.  *       user resizes the window we take that as an order to ignore
  2820.  *       geometry requests coming from inside the window hierarchy.  If
  2821.  *       we accidentally interpret a response to our request as a
  2822.  *       user-initiated action, the window will stop responding to
  2823.  *       new geometry requests.  To make this distinction, (a) this
  2824.  *       procedure sets a flag for TopLevelEventProc to indicate that
  2825.  *       we're waiting to sync with the wm, and (b) all changes to
  2826.  *       the size of a top-level window are followed by calls to this
  2827.  *       procedure.
  2828.  *    2. Races and confusion can come about if there are multiple
  2829.  *       operations outstanding at a time (e.g. two different resizes
  2830.  *       of the top-level window:  it's hard to tell which of the
  2831.  *       ConfigureNotify events coming back is for which request).
  2832.  *    While waiting, all events covered by StructureNotifyMask are
  2833.  *    processed and all others are deferred.
  2834.  *
  2835.  *----------------------------------------------------------------------
  2836.  */
  2837.  
  2838. static void
  2839. WaitForConfigureNotify(winPtr, serial)
  2840.     TkWindow *winPtr;        /* Top-level window for which we want
  2841.                  * to see a ConfigureNotify. */
  2842.     unsigned long serial;    /* Serial number of resize request.  Want to
  2843.                  * be sure wm has seen this. */
  2844. {
  2845.     WmInfo *wmPtr = winPtr->wmInfoPtr;
  2846.     XEvent event;
  2847.     int diff, code;
  2848.     int gotConfig = 0;
  2849.  
  2850.     /*
  2851.      * One more tricky detail about this procedure.  In some cases the
  2852.      * window manager will decide to ignore a configure request (e.g.
  2853.      * because it thinks the window is already in the right place).
  2854.      * To avoid hanging in this situation, only wait for a few seconds,
  2855.      * then give up.
  2856.      */
  2857.  
  2858.     while (!gotConfig) {
  2859.     wmPtr->flags |= WM_SYNC_PENDING;
  2860.     code = WaitForEvent(winPtr->display, winPtr->window, ConfigureNotify,
  2861.         &event);
  2862.     wmPtr->flags &= ~WM_SYNC_PENDING;
  2863.     if (code != TCL_OK) {
  2864.         if (wmTracing) {
  2865.         printf("WaitForConfigureNotify giving up on %s\n",
  2866.             winPtr->pathName);
  2867.         }
  2868.         break;
  2869.     }
  2870.     diff = event.xconfigure.serial - serial;
  2871.     if (diff >= 0) {
  2872.         gotConfig = 1;
  2873.     }
  2874.     }
  2875.     wmPtr->flags &= ~WM_MOVE_PENDING;
  2876.     if (wmTracing) {
  2877.     printf("WaitForConfigureNotify finished with %s, serial %ld\n",
  2878.         winPtr->pathName, serial);
  2879.     }
  2880. }
  2881.  
  2882. /*
  2883.  *----------------------------------------------------------------------
  2884.  *
  2885.  * WaitForEvent --
  2886.  *
  2887.  *    This procedure is used by WaitForConfigureNotify and
  2888.  *    WaitForMapNotify to wait for an event of a certain type
  2889.  *    to arrive.
  2890.  *
  2891.  * Results:
  2892.  *    Under normal conditions, TCL_OK is returned and an event for
  2893.  *    display and window that matches "mask" is stored in *eventPtr.
  2894.  *    This event  has already been processed by Tk before this procedure
  2895.  *    returns.  If a long time goes by with no event of the right type
  2896.  *    arriving, or if an error occurs while waiting for the event to
  2897.  *    arrive, then TCL_ERROR is returned.
  2898.  *
  2899.  * Side effects:
  2900.  *    While waiting for the desired event to occur, Configurenotify
  2901.  *    events for window are processed, as are all ReparentNotify events,
  2902.  *
  2903.  *----------------------------------------------------------------------
  2904.  */
  2905.  
  2906. static int
  2907. WaitForEvent(display, window, type, eventPtr)
  2908.     Display *display;        /* Display event is coming from. */
  2909.     Window window;        /* Window for which event is desired. */
  2910.     int type;            /* Type of event that is wanted. */
  2911.     XEvent *eventPtr;        /* Place to store event. */
  2912. {
  2913. #define TIMEOUT_MS 2000
  2914.     WaitRestrictInfo info;
  2915.     Tk_RestrictProc *oldRestrictProc;
  2916.     ClientData oldRestrictData;
  2917.  
  2918.     /*
  2919.      * Set up an event filter to select just the events we want, and
  2920.      * a timer handler, then wait for events until we get the event
  2921.      * we want or a timeout happens.
  2922.      */
  2923.  
  2924.     info.display = display;
  2925.     info.window = window;
  2926.     info.type = type;
  2927.     info.eventPtr = eventPtr;
  2928.     info.foundEvent = 0;
  2929.     info.timeout = 0;
  2930.     oldRestrictProc = Tk_RestrictEvents(WaitRestrictProc, (ClientData) &info,
  2931.         &oldRestrictData);
  2932.     Tcl_CreateModalTimeout(TIMEOUT_MS, WaitTimeoutProc,
  2933.         (ClientData) &info);
  2934.     while (1) {
  2935.     Tcl_DoOneEvent(TCL_WINDOW_EVENTS);
  2936.     if (info.foundEvent) {
  2937.         break;
  2938.     }
  2939.     if (info.timeout) {
  2940.         break;
  2941.     }
  2942.     }
  2943.     Tcl_DeleteModalTimeout(WaitTimeoutProc, (ClientData) &info);
  2944.     (void) Tk_RestrictEvents(oldRestrictProc, oldRestrictData,
  2945.         &oldRestrictData);
  2946.     if (info.foundEvent) {
  2947.     return TCL_OK;
  2948.     }
  2949.     return TCL_ERROR;
  2950. }
  2951.  
  2952. /*
  2953.  *----------------------------------------------------------------------
  2954.  *
  2955.  * WaitRestrictProc --
  2956.  *
  2957.  *    This procedure is a Tk_RestrictProc that is used to filter
  2958.  *    events while WaitForEvent is active.
  2959.  *
  2960.  * Results:
  2961.  *    Returns TK_PROCESS_EVENT if the right event is found.  Also
  2962.  *    returns TK_PROCESS_EVENT if any ReparentNotify event is found
  2963.  *    for window or if the event is a ConfigureNotify for window.
  2964.  *    Otherwise returns TK_DEFER_EVENT.
  2965.  *
  2966.  * Side effects:
  2967.  *    An event may get stored in the area indicated by the caller
  2968.  *    of WaitForEvent.
  2969.  *
  2970.  *----------------------------------------------------------------------
  2971.  */
  2972.  
  2973. static Tk_RestrictAction
  2974. WaitRestrictProc(clientData, eventPtr)
  2975.     ClientData clientData;    /* Pointer to WaitRestrictInfo structure. */
  2976.     XEvent *eventPtr;        /* Event that is about to be handled. */
  2977. {
  2978.     WaitRestrictInfo *infoPtr = (WaitRestrictInfo *) clientData;
  2979.  
  2980.     if (eventPtr->type == ReparentNotify) {
  2981.     return TK_PROCESS_EVENT;
  2982.     }
  2983.     if ((eventPtr->xany.window != infoPtr->window)
  2984.         || (eventPtr->xany.display != infoPtr->display)) {
  2985.     return TK_DEFER_EVENT;
  2986.     }
  2987.     if (eventPtr->type == infoPtr->type) {
  2988.     *infoPtr->eventPtr = *eventPtr;
  2989.     infoPtr->foundEvent = 1;
  2990.     return TK_PROCESS_EVENT;
  2991.     }
  2992.     if (eventPtr->type == ConfigureNotify) {
  2993.     return TK_PROCESS_EVENT;
  2994.     }
  2995.     return TK_DEFER_EVENT;
  2996. }
  2997.  
  2998. /*
  2999.  *----------------------------------------------------------------------
  3000.  *
  3001.  * WaitTimeoutProc --
  3002.  *
  3003.  *    This procedure is invoked as a timer handler when too much
  3004.  *    time elapses during a call to WaitForEvent.  It sets a flag
  3005.  *    in a structure shared with WaitForEvent so that WaitForEvent
  3006.  *    knows that it should return.
  3007.  *
  3008.  * Results:
  3009.  *    None.
  3010.  *
  3011.  * Side effects:
  3012.  *    The timeout field gest set in the WaitRestrictInfo structure.
  3013.  *
  3014.  *----------------------------------------------------------------------
  3015.  */
  3016.  
  3017. static void
  3018. WaitTimeoutProc(clientData)
  3019.     ClientData clientData;    /* Pointer to WaitRestrictInfo structure. */
  3020. {
  3021.     WaitRestrictInfo *infoPtr = (WaitRestrictInfo *) clientData;
  3022.  
  3023.     infoPtr->timeout = 1;
  3024. }
  3025.  
  3026. /*
  3027.  *----------------------------------------------------------------------
  3028.  *
  3029.  * WaitForMapNotify --
  3030.  *
  3031.  *    This procedure is invoked in order to synchronize with the
  3032.  *    window manager.  It waits for the window's mapped state to
  3033.  *    reach the value given by mapped.
  3034.  *
  3035.  * Results:
  3036.  *    None.
  3037.  *
  3038.  * Side effects:
  3039.  *    Delays the execution of the process until winPtr becomes mapped
  3040.  *    or unmapped, depending on the "mapped" argument.  This allows us
  3041.  *    to synchronize with the window manager, and allows us to
  3042.  *    identify changes in window size that come about when the window
  3043.  *    manager first starts managing the window (as opposed to those
  3044.  *    requested interactively by the user later).  See the comments
  3045.  *    for WaitForConfigureNotify and WM_SYNC_PENDING.  While waiting,
  3046.  *    all events covered by StructureNotifyMask are processed and all
  3047.  *    others are deferred.
  3048.  *
  3049.  *----------------------------------------------------------------------
  3050.  */
  3051.  
  3052. static void
  3053. WaitForMapNotify(winPtr, mapped)
  3054.     TkWindow *winPtr;        /* Top-level window for which we want
  3055.                  * to see a particular mapping state. */
  3056.     int mapped;            /* If non-zero, wait for window to become
  3057.                  * mapped, otherwise wait for it to become
  3058.                  * unmapped. */
  3059. {
  3060.     WmInfo *wmPtr = winPtr->wmInfoPtr;
  3061.     XEvent event;
  3062.     int code;
  3063.  
  3064.     while (1) {
  3065.     if (mapped) {
  3066.         if (winPtr->flags & TK_MAPPED) {
  3067.         break;
  3068.         }
  3069.     } else if (!(winPtr->flags & TK_MAPPED)) {
  3070.         break;
  3071.     }
  3072.     wmPtr->flags |= WM_SYNC_PENDING;
  3073.     code = WaitForEvent(winPtr->display, winPtr->window,
  3074.         mapped ? MapNotify : UnmapNotify, &event);
  3075.     wmPtr->flags &= ~WM_SYNC_PENDING;
  3076.     if (code != TCL_OK) {
  3077.         /*
  3078.          * There are some bizarre situations in which the window
  3079.          * manager can't respond or chooses not to (e.g. if we've
  3080.          * got a grab set it can't respond).  If this happens then
  3081.          * just quit.
  3082.          */
  3083.  
  3084.         if (wmTracing) {
  3085.         printf("WaitForMapNotify giving up on %s\n", winPtr->pathName);
  3086.         }
  3087.         break;
  3088.     }
  3089.     }
  3090.     wmPtr->flags &= ~WM_MOVE_PENDING;
  3091.     if (wmTracing) {
  3092.     printf("WaitForMapNotify finished with %s\n", winPtr->pathName);
  3093.     }
  3094. }
  3095.  
  3096. /*
  3097.  *--------------------------------------------------------------
  3098.  *
  3099.  * UpdateHints --
  3100.  *
  3101.  *    This procedure is called to update the window manager's
  3102.  *    hints information from the information in a WmInfo
  3103.  *    structure.
  3104.  *
  3105.  * Results:
  3106.  *    None.
  3107.  *
  3108.  * Side effects:
  3109.  *    Properties get changed for winPtr.
  3110.  *
  3111.  *--------------------------------------------------------------
  3112.  */
  3113.  
  3114. static void
  3115. UpdateHints(winPtr)
  3116.     TkWindow *winPtr;
  3117. {
  3118.     WmInfo *wmPtr = winPtr->wmInfoPtr;
  3119.  
  3120.     if (wmPtr->flags & WM_NEVER_MAPPED) {
  3121.     return;
  3122.     }
  3123.     XSetWMHints(winPtr->display, winPtr->window, &wmPtr->hints);
  3124. }
  3125.  
  3126. /*
  3127.  *--------------------------------------------------------------
  3128.  *
  3129.  * ParseGeometry --
  3130.  *
  3131.  *    This procedure parses a geometry string and updates
  3132.  *    information used to control the geometry of a top-level
  3133.  *    window.
  3134.  *
  3135.  * Results:
  3136.  *    A standard Tcl return value, plus an error message in
  3137.  *    interp->result if an error occurs.
  3138.  *
  3139.  * Side effects:
  3140.  *    The size and/or location of winPtr may change.
  3141.  *
  3142.  *--------------------------------------------------------------
  3143.  */
  3144.  
  3145. static int
  3146. ParseGeometry(interp, string, winPtr)
  3147.     Tcl_Interp *interp;        /* Used for error reporting. */
  3148.     char *string;        /* String containing new geometry.  Has the
  3149.                  * standard form "=wxh+x+y". */
  3150.     TkWindow *winPtr;        /* Pointer to top-level window whose
  3151.                  * geometry is to be changed. */
  3152. {
  3153.     register WmInfo *wmPtr = winPtr->wmInfoPtr;
  3154.     int x, y, width, height, flags;
  3155.     char *end;
  3156.     register char *p = string;
  3157.  
  3158.     /*
  3159.      * The leading "=" is optional.
  3160.      */
  3161.  
  3162.     if (*p == '=') {
  3163.     p++;
  3164.     }
  3165.  
  3166.     /*
  3167.      * Parse the width and height, if they are present.  Don't
  3168.      * actually update any of the fields of wmPtr until we've
  3169.      * successfully parsed the entire geometry string.
  3170.      */
  3171.  
  3172.     width = wmPtr->width;
  3173.     height = wmPtr->height;
  3174.     x = wmPtr->x;
  3175.     y = wmPtr->y;
  3176.     flags = wmPtr->flags;
  3177.     if (isdigit(UCHAR(*p))) {
  3178.     width = strtoul(p, &end, 10);
  3179.     p = end;
  3180.     if (*p != 'x') {
  3181.         goto error;
  3182.     }
  3183.     p++;
  3184.     if (!isdigit(UCHAR(*p))) {
  3185.         goto error;
  3186.     }
  3187.     height = strtoul(p, &end, 10);
  3188.     p = end;
  3189.     }
  3190.  
  3191.     /*
  3192.      * Parse the X and Y coordinates, if they are present.
  3193.      */
  3194.  
  3195.     if (*p != '\0') {
  3196.     flags &= ~(WM_NEGATIVE_X | WM_NEGATIVE_Y);
  3197.     if (*p == '-') {
  3198.         flags |= WM_NEGATIVE_X;
  3199.     } else if (*p != '+') {
  3200.         goto error;
  3201.     }
  3202.     x = strtol(p+1, &end, 10);
  3203.     p = end;
  3204.     if (*p == '-') {
  3205.         flags |= WM_NEGATIVE_Y;
  3206.     } else if (*p != '+') {
  3207.         goto error;
  3208.     }
  3209.     y = strtol(p+1, &end, 10);
  3210.     if (*end != '\0') {
  3211.         goto error;
  3212.     }
  3213.  
  3214.     /*
  3215.      * Assume that the geometry information came from the user,
  3216.      * unless an explicit source has been specified.  Otherwise
  3217.      * most window managers assume that the size hints were
  3218.      * program-specified and they ignore them.
  3219.      */
  3220.  
  3221.     if ((wmPtr->sizeHintsFlags & (USPosition|PPosition)) == 0) {
  3222.         wmPtr->sizeHintsFlags |= USPosition;
  3223.         flags |= WM_UPDATE_SIZE_HINTS;
  3224.     }
  3225.     }
  3226.  
  3227.     /*
  3228.      * Everything was parsed OK.  Update the fields of *wmPtr and
  3229.      * arrange for the appropriate information to be percolated out
  3230.      * to the window manager at the next idle moment.
  3231.      */
  3232.  
  3233.     wmPtr->width = width;
  3234.     wmPtr->height = height;
  3235.     wmPtr->x = x;
  3236.     wmPtr->y = y;
  3237.     flags |= WM_MOVE_PENDING;
  3238.     wmPtr->flags = flags;
  3239.  
  3240.     if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
  3241.     Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) winPtr);
  3242.     wmPtr->flags |= WM_UPDATE_PENDING;
  3243.     }
  3244.     return TCL_OK;
  3245.  
  3246.     error:
  3247.     Tcl_AppendResult(interp, "bad geometry specifier \"",
  3248.         string, "\"", (char *) NULL);
  3249.     return TCL_ERROR;
  3250. }
  3251.  
  3252. /*
  3253.  *----------------------------------------------------------------------
  3254.  *
  3255.  * Tk_GetRootCoords --
  3256.  *
  3257.  *    Given a token for a window, this procedure traces through the
  3258.  *    window's lineage to find the (virtual) root-window coordinates
  3259.  *    corresponding to point (0,0) in the window.
  3260.  *
  3261.  * Results:
  3262.  *    The locations pointed to by xPtr and yPtr are filled in with
  3263.  *    the root coordinates of the (0,0) point in tkwin.  If a virtual
  3264.  *    root window is in effect for the window, then the coordinates
  3265.  *    in the virtual root are returned.
  3266.  *
  3267.  * Side effects:
  3268.  *    None.
  3269.  *
  3270.  *----------------------------------------------------------------------
  3271.  */
  3272.  
  3273. void
  3274. Tk_GetRootCoords(tkwin, xPtr, yPtr)
  3275.     Tk_Window tkwin;        /* Token for window. */
  3276.     int *xPtr;            /* Where to store x-displacement of (0,0). */
  3277.     int *yPtr;            /* Where to store y-displacement of (0,0). */
  3278. {
  3279.     int x, y;
  3280.     register TkWindow *winPtr = (TkWindow *) tkwin;
  3281.  
  3282.     /*
  3283.      * Search back through this window's parents all the way to a
  3284.      * top-level window, combining the offsets of each window within
  3285.      * its parent.
  3286.      */
  3287.  
  3288.     x = y = 0;
  3289.     while (1) {
  3290.     x += winPtr->changes.x + winPtr->changes.border_width;
  3291.     y += winPtr->changes.y + winPtr->changes.border_width;
  3292.     if ((winPtr->flags & TK_TOP_LEVEL) || (winPtr->parentPtr == NULL)) {
  3293.         break;
  3294.     }
  3295.     winPtr = winPtr->parentPtr;
  3296.     }
  3297.     *xPtr = x;
  3298.     *yPtr = y;
  3299. }
  3300.  
  3301. /*
  3302.  *----------------------------------------------------------------------
  3303.  *
  3304.  * Tk_CoordsToWindow --
  3305.  *
  3306.  *    Given the (virtual) root coordinates of a point, this procedure
  3307.  *    returns the token for the top-most window covering that point,
  3308.  *    if there exists such a window in this application.
  3309.  *
  3310.  * Results:
  3311.  *    The return result is either a token for the window corresponding
  3312.  *    to rootX and rootY, or else NULL to indicate that there is no such
  3313.  *    window.
  3314.  *
  3315.  * Side effects:
  3316.  *    None.
  3317.  *
  3318.  *----------------------------------------------------------------------
  3319.  */
  3320.  
  3321. Tk_Window
  3322. Tk_CoordsToWindow(rootX, rootY, tkwin)
  3323.     int rootX, rootY;        /* Coordinates of point in root window.  If
  3324.                  * a virtual-root window manager is in use,
  3325.                  * these coordinates refer to the virtual
  3326.                  * root, not the real root. */
  3327.     Tk_Window tkwin;        /* Token for any window in application;
  3328.                  * used to identify the display. */
  3329. {
  3330.     Window rootChild, root, vRoot;
  3331.     int dummy1, dummy2;
  3332.     register WmInfo *wmPtr;
  3333.     register TkWindow *winPtr, *childPtr;
  3334.     TkWindow *nextPtr;        /* Coordinates of highest child found so
  3335.                  * far that contains point. */
  3336.     int x, y;            /* Coordinates in winPtr. */
  3337.     int tmpx, tmpy, bd;
  3338.  
  3339.     /*
  3340.      * Step 1: find any top-level window for the right screen.
  3341.      */
  3342.  
  3343.     while (!Tk_IsTopLevel(tkwin)) {
  3344.     tkwin = Tk_Parent(tkwin);
  3345.     }
  3346.     wmPtr = ((TkWindow *) tkwin)->wmInfoPtr;
  3347.  
  3348.     /*
  3349.      * Step 2: find the window in the actual root that contains the
  3350.      * desired point.  Special trick:  if a virtual root window manager
  3351.      * is in use, there may be windows in both the true root (e.g.
  3352.      * pop-up menus) and in the virtual root;  have to look in *both*
  3353.      * places.
  3354.      */
  3355.  
  3356.     UpdateVRootGeometry(wmPtr);
  3357.     root = RootWindowOfScreen(Tk_Screen(tkwin));
  3358.     if (XTranslateCoordinates(Tk_Display(tkwin), root, root,
  3359.         rootX + wmPtr->vRootX, rootY + wmPtr->vRootY,
  3360.         &dummy1, &dummy2, &rootChild) == False) {
  3361.     panic("Tk_CoordsToWindow get False return from XTranslateCoordinates");
  3362.     }
  3363.  
  3364.     /*
  3365.      * Step 3: if the window we've found so far (a child of the root)
  3366.      * is the virtual root window, then look again to find the child of
  3367.      * the virtual root.
  3368.      */
  3369.  
  3370.     vRoot = ((TkWindow *) tkwin)->wmInfoPtr->vRoot;
  3371.     if ((vRoot != None) && (rootChild == vRoot)) {
  3372.     if (XTranslateCoordinates(Tk_Display(tkwin), vRoot, vRoot, rootX,
  3373.         rootY, &dummy1, &dummy2, &rootChild) == False) {
  3374.         panic("Tk_CoordsToWindow get False return from XTranslateCoordinates");
  3375.     }
  3376.     }
  3377.     for (wmPtr = firstWmPtr; ; wmPtr = wmPtr->nextPtr) {
  3378.     if (wmPtr == NULL) {
  3379.         return NULL;
  3380.     }
  3381.     if ((wmPtr->reparent == rootChild) || ((wmPtr->reparent == None)
  3382.         && (wmPtr->winPtr->window == rootChild))) {
  3383.         break;
  3384.     }
  3385.     }
  3386.     winPtr = wmPtr->winPtr;
  3387.     if (winPtr->mainPtr != ((TkWindow *) tkwin)->mainPtr) {
  3388.     return NULL;
  3389.     }
  3390.  
  3391.     /*
  3392.      * Step 4: work down through the hierarchy underneath this window.
  3393.      * At each level, scan through all the children to find the highest
  3394.      * one in the stacking order that contains the point.  Then repeat
  3395.      * the whole process on that child.
  3396.      */
  3397.  
  3398.     x = rootX;
  3399.     y = rootY;
  3400.     while (1) {
  3401.     x -= winPtr->changes.x;
  3402.     y -= winPtr->changes.y;
  3403.     nextPtr = NULL;
  3404.     for (childPtr = winPtr->childList; childPtr != NULL;
  3405.         childPtr = childPtr->nextPtr) {
  3406.         if (!Tk_IsMapped(childPtr) || (childPtr->flags & TK_TOP_LEVEL)) {
  3407.         continue;
  3408.         }
  3409.         tmpx = x - childPtr->changes.x;
  3410.         tmpy = y - childPtr->changes.y;
  3411.         bd = childPtr->changes.border_width;
  3412.         if ((tmpx >= -bd) && (tmpy >= -bd)
  3413.             && (tmpx < (childPtr->changes.width + bd))
  3414.             && (tmpy < (childPtr->changes.height + bd))) {
  3415.         nextPtr = childPtr;
  3416.         }
  3417.     }
  3418.     if (nextPtr == NULL) {
  3419.         break;
  3420.     }
  3421.     winPtr = nextPtr;
  3422.     }
  3423.     return (Tk_Window) winPtr;
  3424. }
  3425.  
  3426. /*
  3427.  *----------------------------------------------------------------------
  3428.  *
  3429.  * UpdateVRootGeometry --
  3430.  *
  3431.  *    This procedure is called to update all the virtual root
  3432.  *    geometry information in wmPtr.
  3433.  *
  3434.  * Results:
  3435.  *    None.
  3436.  *
  3437.  * Side effects:
  3438.  *    The vRootX, vRootY, vRootWidth, and vRootHeight fields in
  3439.  *    wmPtr are filled with the most up-to-date information.
  3440.  *
  3441.  *----------------------------------------------------------------------
  3442.  */
  3443.  
  3444. static void
  3445. UpdateVRootGeometry(wmPtr)
  3446.     WmInfo *wmPtr;        /* Window manager information to be
  3447.                  * updated.  The wmPtr->vRoot field must
  3448.                  * be valid. */
  3449. {
  3450.     TkWindow *winPtr = wmPtr->winPtr;
  3451.     int bd;
  3452.     unsigned int dummy;
  3453.     Window dummy2;
  3454.     Status status;
  3455.     Tk_ErrorHandler handler;
  3456.  
  3457.     /*
  3458.      * If this isn't a virtual-root window manager, just return information
  3459.      * about the screen.
  3460.      */
  3461.  
  3462.     wmPtr->flags &= ~WM_VROOT_OFFSET_STALE;
  3463.     if (wmPtr->vRoot == None) {
  3464.     noVRoot:
  3465.     wmPtr->vRootX = wmPtr->vRootY = 0;
  3466.     wmPtr->vRootWidth = DisplayWidth(winPtr->display, winPtr->screenNum);
  3467.     wmPtr->vRootHeight = DisplayHeight(winPtr->display, winPtr->screenNum);
  3468.     return;
  3469.     }
  3470.  
  3471.     /*
  3472.      * Refresh the virtual root information if it's out of date.
  3473.      */
  3474.  
  3475.     handler = Tk_CreateErrorHandler(winPtr->display, -1, -1, -1,
  3476.         (Tk_ErrorProc *) NULL, (ClientData) NULL);
  3477.     status = XGetGeometry(winPtr->display, wmPtr->vRoot,
  3478.         &dummy2, &wmPtr->vRootX, &wmPtr->vRootY,
  3479.         (unsigned int *) &wmPtr->vRootWidth,
  3480.         (unsigned int *) &wmPtr->vRootHeight, (unsigned int *) &bd,
  3481.         &dummy);
  3482.     if (wmTracing) {
  3483.     printf("UpdateVRootGeometry: x = %d, y = %d, width = %d, ",
  3484.         wmPtr->vRootX, wmPtr->vRootY, wmPtr->vRootWidth);
  3485.     printf("height = %d, status = %d\n", wmPtr->vRootHeight, status);
  3486.     }
  3487.     Tk_DeleteErrorHandler(handler);
  3488.     if (status == 0) {
  3489.     /*
  3490.      * The virtual root is gone!  Pretend that it never existed.
  3491.      */
  3492.  
  3493.     wmPtr->vRoot = None;
  3494.     goto noVRoot;
  3495.     }
  3496. }
  3497.  
  3498. /*
  3499.  *----------------------------------------------------------------------
  3500.  *
  3501.  * Tk_GetVRootGeometry --
  3502.  *
  3503.  *    This procedure returns information about the virtual root
  3504.  *    window corresponding to a particular Tk window.
  3505.  *
  3506.  * Results:
  3507.  *    The values at xPtr, yPtr, widthPtr, and heightPtr are set
  3508.  *    with the offset and dimensions of the root window corresponding
  3509.  *    to tkwin.  If tkwin is being managed by a virtual root window
  3510.  *    manager these values correspond to the virtual root window being
  3511.  *    used for tkwin;  otherwise the offsets will be 0 and the
  3512.  *    dimensions will be those of the screen.
  3513.  *
  3514.  * Side effects:
  3515.  *    Vroot window information is refreshed if it is out of date.
  3516.  *
  3517.  *----------------------------------------------------------------------
  3518.  */
  3519.  
  3520. void
  3521. Tk_GetVRootGeometry(tkwin, xPtr, yPtr, widthPtr, heightPtr)
  3522.     Tk_Window tkwin;        /* Window whose virtual root is to be
  3523.                  * queried. */
  3524.     int *xPtr, *yPtr;        /* Store x and y offsets of virtual root
  3525.                  * here. */
  3526.     int *widthPtr, *heightPtr;    /* Store dimensions of virtual root here. */
  3527. {
  3528.     WmInfo *wmPtr;
  3529.     TkWindow *winPtr = (TkWindow *) tkwin;
  3530.  
  3531.     /*
  3532.      * Find the top-level window for tkwin, and locate the window manager
  3533.      * information for that window.
  3534.      */
  3535.  
  3536.     while (!(winPtr->flags & TK_TOP_LEVEL) && (winPtr->parentPtr != NULL)) {
  3537.     winPtr = winPtr->parentPtr;
  3538.     }
  3539.     wmPtr = winPtr->wmInfoPtr;
  3540.  
  3541.     /*
  3542.      * Make sure that the geometry information is up-to-date, then copy
  3543.      * it out to the caller.
  3544.      */
  3545.  
  3546.     if (wmPtr->flags & WM_VROOT_OFFSET_STALE) {
  3547.     UpdateVRootGeometry(wmPtr);
  3548.     }
  3549.     *xPtr = wmPtr->vRootX;
  3550.     *yPtr = wmPtr->vRootY;
  3551.     *widthPtr = wmPtr->vRootWidth;
  3552.     *heightPtr = wmPtr->vRootHeight;
  3553. }
  3554.  
  3555. /*
  3556.  *----------------------------------------------------------------------
  3557.  *
  3558.  * Tk_MoveToplevelWindow --
  3559.  *
  3560.  *    This procedure is called instead of Tk_MoveWindow to adjust
  3561.  *    the x-y location of a top-level window.  It delays the actual
  3562.  *    move to a later time and keeps window-manager information
  3563.  *    up-to-date with the move
  3564.  *
  3565.  * Results:
  3566.  *    None.
  3567.  *
  3568.  * Side effects:
  3569.  *    The window is eventually moved so that its upper-left corner
  3570.  *    (actually, the upper-left corner of the window's decorative
  3571.  *    frame, if there is one) is at (x,y).
  3572.  *
  3573.  *----------------------------------------------------------------------
  3574.  */
  3575.  
  3576. void
  3577. Tk_MoveToplevelWindow(tkwin, x, y)
  3578.     Tk_Window tkwin;        /* Window to move. */
  3579.     int x, y;            /* New location for window (within
  3580.                  * parent). */
  3581. {
  3582.     TkWindow *winPtr = (TkWindow *) tkwin;
  3583.     register WmInfo *wmPtr = winPtr->wmInfoPtr;
  3584.  
  3585.     if (!(winPtr->flags & TK_TOP_LEVEL)) {
  3586.     panic("Tk_MoveToplevelWindow called with non-toplevel window");
  3587.     }
  3588.     wmPtr->x = x;
  3589.     wmPtr->y = y;
  3590.     wmPtr->flags |= WM_MOVE_PENDING;
  3591.     wmPtr->flags &= ~(WM_NEGATIVE_X|WM_NEGATIVE_Y);
  3592.     if ((wmPtr->sizeHintsFlags & (USPosition|PPosition)) == 0) {
  3593.     wmPtr->sizeHintsFlags |= USPosition;
  3594.     wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
  3595.     }
  3596.  
  3597.     /*
  3598.      * If the window has already been mapped, must bring its geometry
  3599.      * up-to-date immediately, otherwise an event might arrive from the
  3600.      * server that would overwrite wmPtr->x and wmPtr->y and lose the
  3601.      * new position.
  3602.      */
  3603.  
  3604.     if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  3605.     if (wmPtr->flags & WM_UPDATE_PENDING) {
  3606.         Tcl_CancelIdleCall(UpdateGeometryInfo, (ClientData) winPtr);
  3607.     }
  3608.     UpdateGeometryInfo((ClientData) winPtr);
  3609.     }
  3610. }
  3611.  
  3612. /*
  3613.  *----------------------------------------------------------------------
  3614.  *
  3615.  * UpdateWmProtocols --
  3616.  *
  3617.  *    This procedure transfers the most up-to-date information about
  3618.  *    window manager protocols from the WmInfo structure to the actual
  3619.  *    property on the top-level window.
  3620.  *
  3621.  * Results:
  3622.  *    None.
  3623.  *
  3624.  * Side effects:
  3625.  *    The WM_PROTOCOLS property gets changed for wmPtr's window.
  3626.  *
  3627.  *----------------------------------------------------------------------
  3628.  */
  3629.  
  3630. static void
  3631. UpdateWmProtocols(wmPtr)
  3632.     register WmInfo *wmPtr;    /* Information about top-level window. */
  3633. {
  3634.     register ProtocolHandler *protPtr;
  3635.     Atom deleteWindowAtom;
  3636.     int count;
  3637.     Atom *arrayPtr, *atomPtr;
  3638.  
  3639.     /*
  3640.      * There are only two tricky parts here.  First, there could be any
  3641.      * number of atoms for the window, so count them and malloc an array
  3642.      * to hold all of their atoms.  Second, we *always* want to respond
  3643.      * to the WM_DELETE_WINDOW protocol, even if no-one's officially asked.
  3644.      */
  3645.  
  3646.     for (protPtr = wmPtr->protPtr, count = 1; protPtr != NULL;
  3647.         protPtr = protPtr->nextPtr, count++) {
  3648.     /* Empty loop body;  we're just counting the handlers. */
  3649.     }
  3650.     arrayPtr = (Atom *) ckalloc((unsigned) (count * sizeof(Atom)));
  3651.     deleteWindowAtom = Tk_InternAtom((Tk_Window) wmPtr->winPtr,
  3652.         "WM_DELETE_WINDOW");
  3653.     arrayPtr[0] = deleteWindowAtom;
  3654.     for (protPtr = wmPtr->protPtr, atomPtr = &arrayPtr[1];
  3655.         protPtr != NULL; protPtr = protPtr->nextPtr) {
  3656.     if (protPtr->protocol != deleteWindowAtom) {
  3657.         *atomPtr = protPtr->protocol;
  3658.         atomPtr++;
  3659.     }
  3660.     }
  3661.     XChangeProperty(wmPtr->winPtr->display, wmPtr->winPtr->window,
  3662.         Tk_InternAtom((Tk_Window) wmPtr->winPtr, "WM_PROTOCOLS"),
  3663.         XA_ATOM, 32, PropModeReplace, (unsigned char *) arrayPtr,
  3664.         atomPtr-arrayPtr);
  3665.     ckfree((char *) arrayPtr);
  3666. }
  3667.  
  3668. /*
  3669.  *----------------------------------------------------------------------
  3670.  *
  3671.  * TkWmProtocolEventProc --
  3672.  *
  3673.  *    This procedure is called by the Tk_HandleEvent whenever a
  3674.  *    ClientMessage event arrives whose type is "WM_PROTOCOLS".
  3675.  *    This procedure handles the message from the window manager
  3676.  *    in an appropriate fashion.
  3677.  *
  3678.  * Results:
  3679.  *    None.
  3680.  *
  3681.  * Side effects:
  3682.  *    Depends on what sort of handler, if any, was set up for the
  3683.  *    protocol.
  3684.  *
  3685.  *----------------------------------------------------------------------
  3686.  */
  3687.  
  3688. void
  3689. TkWmProtocolEventProc(winPtr, eventPtr)
  3690.     TkWindow *winPtr;        /* Window to which the event was sent. */
  3691.     XEvent *eventPtr;        /* X event. */
  3692. {
  3693.     WmInfo *wmPtr;
  3694.     register ProtocolHandler *protPtr;
  3695.     Atom protocol;
  3696.     int result;
  3697.     char *protocolName;
  3698.     Tcl_Interp *interp;
  3699.  
  3700.     wmPtr = winPtr->wmInfoPtr;
  3701.     if (wmPtr == NULL) {
  3702.     return;
  3703.     }
  3704.     protocol = (Atom) eventPtr->xclient.data.l[0];
  3705.  
  3706.     /*
  3707.      * Note: it's very important to retrieve the protocol name now,
  3708.      * before invoking the command, even though the name won't be used
  3709.      * until after the command returns.  This is because the command
  3710.      * could delete winPtr, making it impossible for us to use it
  3711.      * later in the call to Tk_GetAtomName.
  3712.      */
  3713.  
  3714.     protocolName = Tk_GetAtomName((Tk_Window) winPtr, protocol);
  3715.     for (protPtr = wmPtr->protPtr; protPtr != NULL;
  3716.         protPtr = protPtr->nextPtr) {
  3717.     if (protocol == protPtr->protocol) {
  3718.         Tcl_Preserve((ClientData) protPtr);
  3719.             interp = protPtr->interp;
  3720.             Tcl_Preserve((ClientData) interp);
  3721.         result = Tcl_GlobalEval(interp, protPtr->command);
  3722.         if (result != TCL_OK) {
  3723.         Tcl_AddErrorInfo(interp, "\n    (command for \"");
  3724.         Tcl_AddErrorInfo(interp, protocolName);
  3725.         Tcl_AddErrorInfo(interp,
  3726.             "\" window manager protocol)");
  3727.         Tcl_BackgroundError(interp);
  3728.         }
  3729.             Tcl_Release((ClientData) interp);
  3730.         Tcl_Release((ClientData) protPtr);
  3731.         return;
  3732.     }
  3733.     }
  3734.  
  3735.     /*
  3736.      * No handler was present for this protocol.  If this is a
  3737.      * WM_DELETE_WINDOW message then just destroy the window.
  3738.      */
  3739.  
  3740.     if (protocol == Tk_InternAtom((Tk_Window) winPtr, "WM_DELETE_WINDOW")) {
  3741.     Tk_DestroyWindow((Tk_Window) winPtr);
  3742.     }
  3743. }
  3744.  
  3745. /*
  3746.  *----------------------------------------------------------------------
  3747.  *
  3748.  * TkWmRestackToplevel --
  3749.  *
  3750.  *    This procedure restacks a top-level window.
  3751.  *
  3752.  * Results:
  3753.  *    None.
  3754.  *
  3755.  * Side effects:
  3756.  *    WinPtr gets restacked  as specified by aboveBelow and otherPtr.
  3757.  *    This procedure doesn't return until the restack has taken
  3758.  *    effect and the ConfigureNotify event for it has been received.
  3759.  *
  3760.  *----------------------------------------------------------------------
  3761.  */
  3762.  
  3763. void
  3764. TkWmRestackToplevel(winPtr, aboveBelow, otherPtr)
  3765.     TkWindow *winPtr;        /* Window to restack. */
  3766.     int aboveBelow;        /* Gives relative position for restacking;
  3767.                  * must be Above or Below. */
  3768.     TkWindow *otherPtr;        /* Window relative to which to restack;
  3769.                  * if NULL, then winPtr gets restacked
  3770.                  * above or below *all* siblings. */
  3771. {
  3772.     XWindowChanges changes;
  3773.     XWindowAttributes atts;
  3774.     unsigned int mask;
  3775.     Window window, dummy1, dummy2, vRoot;
  3776.     Window *children;
  3777.     unsigned int numChildren;
  3778.     int i;
  3779.     int desiredIndex = 0;    /* Initialized to stop gcc warnings. */
  3780.     int ourIndex = 0;        /* Initialized to stop gcc warnings. */
  3781.     unsigned long serial;
  3782.     XEvent event;
  3783.     int diff;
  3784.     Tk_ErrorHandler handler;
  3785.  
  3786.     changes.stack_mode = aboveBelow;
  3787.     changes.sibling = None;
  3788.     mask = CWStackMode;
  3789.     if (winPtr->window == None) {
  3790.     Tk_MakeWindowExist((Tk_Window) winPtr);
  3791.     }
  3792.     if (winPtr->wmInfoPtr->flags & WM_NEVER_MAPPED) {
  3793.     /*
  3794.      * Can't set stacking order properly until the window is on the
  3795.      * screen (mapping it may give it a reparent window), so make sure
  3796.      * it's on the screen.
  3797.      */
  3798.  
  3799.     TkWmMapWindow(winPtr);
  3800.     }
  3801.     window = (winPtr->wmInfoPtr->reparent != None)
  3802.         ? winPtr->wmInfoPtr->reparent : winPtr->window;
  3803.     if (otherPtr != NULL) {
  3804.     if (otherPtr->window == None) {
  3805.         Tk_MakeWindowExist((Tk_Window) otherPtr);
  3806.     }
  3807.     if (otherPtr->wmInfoPtr->flags & WM_NEVER_MAPPED) {
  3808.         TkWmMapWindow(otherPtr);
  3809.     }
  3810.     changes.sibling = (otherPtr->wmInfoPtr->reparent != None)
  3811.         ? otherPtr->wmInfoPtr->reparent : otherPtr->window;
  3812.     mask = CWStackMode|CWSibling;
  3813.     }
  3814.  
  3815.     /*
  3816.      * Before actually reconfiguring the window, see if it's already
  3817.      * in the right place.  If so then don't reconfigure it.  The
  3818.      * reason for this extra work is that some window managers will
  3819.      * ignore the reconfigure request if the window is already in
  3820.      * the right place, causing a long delay in WaitForConfigureNotify
  3821.      * while it times out.  Special note: if the window is almost in
  3822.      * the right place, and the only windows between it and the right
  3823.      * place aren't mapped, then we don't reconfigure it either, for
  3824.      * the same reason.
  3825.      */
  3826.  
  3827.     vRoot = winPtr->wmInfoPtr->vRoot;
  3828.     if (vRoot == None) {
  3829.     vRoot = RootWindowOfScreen(Tk_Screen((Tk_Window) winPtr));
  3830.     }
  3831.     if (XQueryTree(winPtr->display, vRoot, &dummy1, &dummy2,
  3832.         &children, &numChildren) != 0) {
  3833.     /*
  3834.      * Find where our window is in the stacking order, and
  3835.      * compute the desired location in the stacking order.
  3836.      */
  3837.  
  3838.     for (i = 0; i < numChildren; i++) {
  3839.         if (children[i] == window) {
  3840.         ourIndex = i;
  3841.         }
  3842.         if (children[i] == changes.sibling) {
  3843.         desiredIndex = i;
  3844.         }
  3845.     }
  3846.     if (mask & CWSibling) {
  3847.         if (aboveBelow == Above) {
  3848.         if (desiredIndex < ourIndex) {
  3849.             desiredIndex += 1;
  3850.         }
  3851.         } else {
  3852.         if (desiredIndex > ourIndex) {
  3853.             desiredIndex -= 1;
  3854.         }
  3855.         }
  3856.     } else {
  3857.         if (aboveBelow == Above) {
  3858.         desiredIndex = numChildren-1;
  3859.         } else {
  3860.         desiredIndex = 0;
  3861.         }
  3862.     }
  3863.  
  3864.     /*
  3865.      * See if there are any mapped windows between where we are
  3866.      * and where we want to be.
  3867.      */
  3868.  
  3869.     handler = Tk_CreateErrorHandler(winPtr->display, -1, -1, -1,
  3870.         (Tk_ErrorProc *) NULL, (ClientData) NULL);
  3871.     while (desiredIndex != ourIndex) {
  3872.         if ((XGetWindowAttributes(winPtr->display, children[desiredIndex],
  3873.             &atts) != 0) && (atts.map_state != IsUnmapped)) {
  3874.         break;
  3875.         }
  3876.         if (desiredIndex < ourIndex) {
  3877.         desiredIndex++;
  3878.         } else {
  3879.         desiredIndex--;
  3880.         }
  3881.     }
  3882.     Tk_DeleteErrorHandler(handler);
  3883.     XFree((char *) children);
  3884.     if (ourIndex == desiredIndex) {
  3885.         return;
  3886.     }
  3887.     }
  3888.  
  3889.     /*
  3890.      * Reconfigure the window.  This tricky because of two things:
  3891.      * (a) Some window managers, like olvwm, insist that we raise
  3892.      *     or lower the toplevel window itself, as opposed to its
  3893.      *     decorative frame.  Attempts to raise or lower the frame
  3894.      *     are ignored.
  3895.      * (b) If the raise or lower is relative to a sibling, X will
  3896.      *     generate an error unless we work with the frames (the
  3897.      *     toplevels themselves aren't siblings).
  3898.      * Fortunately, the procedure XReconfigureWMWindow is supposed
  3899.      * to handle all of this stuff, so be careful to use it instead
  3900.      * of XConfigureWindow.
  3901.      */
  3902.  
  3903.     serial = NextRequest(winPtr->display);
  3904.     if (window != winPtr->window) {
  3905.     XSelectInput(winPtr->display, window, StructureNotifyMask);
  3906.     }
  3907.     XReconfigureWMWindow(winPtr->display, winPtr->window,
  3908.         Tk_ScreenNumber((Tk_Window) winPtr), mask,  &changes);
  3909.  
  3910.     /*
  3911.      * Wait for the reconfiguration to complete.  If we don't wait, then
  3912.      * the window may not restack for a while and the application might
  3913.      * observe it before it has restacked.  Waiting for the reconfiguration
  3914.      * is tricky if winPtr has been reparented, since the window getting
  3915.      * the event isn't one that Tk owns.
  3916.      */
  3917.  
  3918.     if (window == winPtr->window) {
  3919.     WaitForConfigureNotify(winPtr, serial);
  3920.     } else {
  3921.     while (1) {
  3922.         if (WaitForEvent(winPtr->display, window, ConfigureNotify,
  3923.             &event) != TCL_OK) {
  3924.         break;
  3925.         }
  3926.         diff = event.xconfigure.serial - serial;
  3927.         if (diff >= 0) {
  3928.         break;
  3929.         }
  3930.     }
  3931.     XSelectInput(winPtr->display, window, (long) 0);
  3932.     }
  3933. }
  3934.  
  3935. /*
  3936.  *----------------------------------------------------------------------
  3937.  *
  3938.  * TkWmAddToColormapWindows --
  3939.  *
  3940.  *    This procedure is called to add a given window to the
  3941.  *    WM_COLORMAP_WINDOWS property for its top-level, if it
  3942.  *    isn't already there.  It is invoked by the Tk code that
  3943.  *    creates a new colormap, in order to make sure that colormap
  3944.  *    information is propagated to the window manager by default.
  3945.  *
  3946.  * Results:
  3947.  *    None.
  3948.  *
  3949.  * Side effects:
  3950.  *    WinPtr's window gets added to the WM_COLORMAP_WINDOWS
  3951.  *    property of its nearest top-level ancestor, unless the
  3952.  *    colormaps have been set explicitly with the
  3953.  *    "wm colormapwindows" command.
  3954.  *
  3955.  *----------------------------------------------------------------------
  3956.  */
  3957.  
  3958. void
  3959. TkWmAddToColormapWindows(winPtr)
  3960.     TkWindow *winPtr;        /* Window with a non-default colormap.
  3961.                  * Should not be a top-level window. */
  3962. {
  3963.     TkWindow *topPtr;
  3964.     Window *oldPtr, *newPtr;
  3965.     int count, i;
  3966.  
  3967.     if (winPtr->window == None) {
  3968.     return;
  3969.     }
  3970.  
  3971.     for (topPtr = winPtr->parentPtr; ; topPtr = topPtr->parentPtr) {
  3972.     if (topPtr == NULL) {
  3973.         /*
  3974.          * Window is being deleted.  Skip the whole operation.
  3975.          */
  3976.  
  3977.         return;
  3978.     }
  3979.     if (topPtr->flags & TK_TOP_LEVEL) {
  3980.         break;
  3981.     }
  3982.     }
  3983.     if (topPtr->wmInfoPtr->flags & WM_COLORMAPS_EXPLICIT) {
  3984.     return;
  3985.     }
  3986.  
  3987.     /*
  3988.      * Fetch the old value of the property.
  3989.      */
  3990.  
  3991.     if (XGetWMColormapWindows(topPtr->display, topPtr->window,
  3992.         &oldPtr, &count) == 0) {
  3993.     oldPtr = NULL;
  3994.     count = 0;
  3995.     }
  3996.  
  3997.     /*
  3998.      * Make sure that the window isn't already in the list.
  3999.      */
  4000.  
  4001.     for (i = 0; i < count; i++) {
  4002.     if (oldPtr[i] == winPtr->window) {
  4003.         return;
  4004.     }
  4005.     }
  4006.  
  4007.     /*
  4008.      * Make a new bigger array and use it to reset the property.
  4009.      * Automatically add the toplevel itself as the last element
  4010.      * of the list.
  4011.      */
  4012.  
  4013.     newPtr = (Window *) ckalloc((unsigned) ((count+2)*sizeof(Window)));
  4014.     for (i = 0; i < count; i++) {
  4015.     newPtr[i] = oldPtr[i];
  4016.     }
  4017.     if (count == 0) {
  4018.     count++;
  4019.     }
  4020.     newPtr[count-1] = winPtr->window;
  4021.     newPtr[count] = topPtr->window;
  4022.     XSetWMColormapWindows(topPtr->display, topPtr->window, newPtr, count+1);
  4023.     ckfree((char *) newPtr);
  4024.     if (oldPtr != NULL) {
  4025.     XFree((char *) oldPtr);
  4026.     }
  4027. }
  4028.  
  4029. /*
  4030.  *----------------------------------------------------------------------
  4031.  *
  4032.  * TkWmRemoveFromColormapWindows --
  4033.  *
  4034.  *    This procedure is called to remove a given window from the
  4035.  *    WM_COLORMAP_WINDOWS property for its top-level.  It is invoked
  4036.  *    when windows are deleted.
  4037.  *
  4038.  * Results:
  4039.  *    None.
  4040.  *
  4041.  * Side effects:
  4042.  *    WinPtr's window gets removed from the WM_COLORMAP_WINDOWS
  4043.  *    property of its nearest top-level ancestor, unless the
  4044.  *    top-level itself is being deleted too.
  4045.  *
  4046.  *----------------------------------------------------------------------
  4047.  */
  4048.  
  4049. void
  4050. TkWmRemoveFromColormapWindows(winPtr)
  4051.     TkWindow *winPtr;        /* Window that may be present in
  4052.                  * WM_COLORMAP_WINDOWS property for its
  4053.                  * top-level.  Should not be a top-level
  4054.                  * window. */
  4055. {
  4056.     TkWindow *topPtr;
  4057.     Window *oldPtr;
  4058.     int count, i, j;
  4059.  
  4060.     for (topPtr = winPtr->parentPtr; ; topPtr = topPtr->parentPtr) {
  4061.     if (topPtr == NULL) {
  4062.         /*
  4063.          * Ancestors have been deleted, so skip the whole operation.
  4064.          * Seems like this can't ever happen?
  4065.          */
  4066.  
  4067.         return;
  4068.     }
  4069.     if (topPtr->flags & TK_TOP_LEVEL) {
  4070.         break;
  4071.     }
  4072.     }
  4073.     if (topPtr->flags & TK_ALREADY_DEAD) {
  4074.     /*
  4075.      * Top-level is being deleted, so there's no need to cleanup
  4076.      * the WM_COLORMAP_WINDOWS property.
  4077.      */
  4078.  
  4079.     return;
  4080.     }
  4081.  
  4082.     /*
  4083.      * Fetch the old value of the property.
  4084.      */
  4085.  
  4086.     if (XGetWMColormapWindows(topPtr->display, topPtr->window,
  4087.         &oldPtr, &count) == 0) {
  4088.     return;
  4089.     }
  4090.  
  4091.     /*
  4092.      * Find the window and slide the following ones down to cover
  4093.      * it up.
  4094.      */
  4095.  
  4096.     for (i = 0; i < count; i++) {
  4097.     if (oldPtr[i] == winPtr->window) {
  4098.         for (j = i ; j < count-1; j++) {
  4099.         oldPtr[j] = oldPtr[j+1];
  4100.         }
  4101.         XSetWMColormapWindows(topPtr->display, topPtr->window,
  4102.             oldPtr, count-1);
  4103.         break;
  4104.     }
  4105.     }
  4106.     XFree((char *) oldPtr);
  4107. }
  4108.  
  4109. /*
  4110.  *----------------------------------------------------------------------
  4111.  *
  4112.  * TkGetPointerCoords --
  4113.  *
  4114.  *    Fetch the position of the mouse pointer.
  4115.  *
  4116.  * Results:
  4117.  *    *xPtr and *yPtr are filled in with the (virtual) root coordinates
  4118.  *    of the mouse pointer for tkwin's display.  If the pointer isn't
  4119.  *    on tkwin's screen, then -1 values are returned for both
  4120.  *    coordinates.  The argument tkwin must be a toplevel window.
  4121.  *
  4122.  * Side effects:
  4123.  *    None.
  4124.  *
  4125.  *----------------------------------------------------------------------
  4126.  */
  4127.  
  4128. void
  4129. TkGetPointerCoords(tkwin, xPtr, yPtr)
  4130.     Tk_Window tkwin;        /* Toplevel window that identifies screen
  4131.                  * on which lookup is to be done. */
  4132.     int *xPtr, *yPtr;        /* Store pointer coordinates here. */
  4133. {
  4134.     TkWindow *winPtr = (TkWindow *) tkwin;
  4135.     WmInfo *wmPtr;
  4136.     Window w, root, child;
  4137.     int rootX, rootY;
  4138.     unsigned int mask;
  4139.  
  4140.     wmPtr = winPtr->wmInfoPtr;
  4141.  
  4142.     w = wmPtr->vRoot;
  4143.     if (w == None) {
  4144.     w = RootWindow(winPtr->display, winPtr->screenNum);
  4145.     }
  4146.     if (XQueryPointer(winPtr->display, w, &root, &child, &rootX, &rootY,
  4147.         xPtr, yPtr, &mask) != True) {
  4148.     *xPtr = -1;
  4149.     *yPtr = -1;
  4150.     }
  4151. }
  4152.  
  4153. /*
  4154.  *----------------------------------------------------------------------
  4155.  *
  4156.  * TkMakeWindow --
  4157.  *
  4158.  *    Create an actual window system window object based on the
  4159.  *    current attributes of the specified TkWindow.
  4160.  *
  4161.  * Results:
  4162.  *    Returns the handle to the new window, or None on failure.
  4163.  *
  4164.  * Side effects:
  4165.  *    Creates a new X window.
  4166.  *
  4167.  *----------------------------------------------------------------------
  4168.  */
  4169.  
  4170. Window
  4171. TkMakeWindow(winPtr, parent)
  4172.     TkWindow *winPtr;
  4173.     Window parent;
  4174. {
  4175.     return XCreateWindow(winPtr->display, parent, winPtr->changes.x,
  4176.         winPtr->changes.y, (unsigned) winPtr->changes.width,
  4177.         (unsigned) winPtr->changes.height,
  4178.         (unsigned) winPtr->changes.border_width, winPtr->depth,
  4179.         InputOutput, winPtr->visual, winPtr->dirtyAtts,
  4180.         &winPtr->atts);
  4181. }
  4182.  
  4183. /*
  4184.  *----------------------------------------------------------------------
  4185.  *
  4186.  * GetMaxSize --
  4187.  *
  4188.  *    This procedure computes the current maxWidth and maxHeight
  4189.  *    values for a window, taking into account the possibility
  4190.  *    that they may be defaulted.
  4191.  *
  4192.  * Results:
  4193.  *    The values at *maxWidthPtr and *maxHeightPtr are filled
  4194.  *    in with the maximum allowable dimensions of wmPtr's window,
  4195.  *    in grid units.  If no maximum has been specified for the
  4196.  *    window, then this procedure computes the largest sizes that
  4197.  *    will fit on the screen.
  4198.  *
  4199.  * Side effects:
  4200.  *    None.
  4201.  *
  4202.  *----------------------------------------------------------------------
  4203.  */
  4204.  
  4205. static void
  4206. GetMaxSize(wmPtr, maxWidthPtr, maxHeightPtr)
  4207.     WmInfo *wmPtr;        /* Window manager information for the
  4208.                  * window. */
  4209.     int *maxWidthPtr;        /* Where to store the current maximum
  4210.                  * width of the window. */
  4211.     int *maxHeightPtr;        /* Where to store the current maximum
  4212.                  * height of the window. */
  4213. {
  4214.     int tmp;
  4215.  
  4216.     if (wmPtr->maxWidth > 0) {
  4217.     *maxWidthPtr = wmPtr->maxWidth;
  4218.     } else {
  4219.     /*
  4220.      * Must compute a default width.  Fill up the display, leaving a
  4221.      * bit of extra space for the window manager's borders.
  4222.      */
  4223.  
  4224.     tmp = DisplayWidth(wmPtr->winPtr->display, wmPtr->winPtr->screenNum)
  4225.         - 15;
  4226.     if (wmPtr->gridWin != NULL) {
  4227.         /*
  4228.          * Gridding is turned on;  convert from pixels to grid units.
  4229.          */
  4230.  
  4231.         tmp = wmPtr->reqGridWidth
  4232.             + (tmp - wmPtr->winPtr->reqWidth)/wmPtr->widthInc;
  4233.     }
  4234.     *maxWidthPtr = tmp;
  4235.     }
  4236.     if (wmPtr->maxHeight > 0) {
  4237.     *maxHeightPtr = wmPtr->maxHeight;
  4238.     } else {
  4239.     tmp = DisplayHeight(wmPtr->winPtr->display, wmPtr->winPtr->screenNum)
  4240.         - 30;
  4241.     if (wmPtr->gridWin != NULL) {
  4242.         tmp = wmPtr->reqGridHeight
  4243.             + (tmp - wmPtr->winPtr->reqHeight)/wmPtr->heightInc;
  4244.     }
  4245.     *maxHeightPtr = tmp;
  4246.     }
  4247. }
  4248.